Home  >  Article  >  Backend Development  >  How to convert string into hex in python

How to convert string into hex in python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-26 10:09:4912454browse

How to convert string into hex in python

How to change string into hex in python? Hex is a hexadecimal number. The following is an introduction to various type conversions (int, str, chr, hex, oct, etc.) in python:

int(x [,base ] )                                                                                                                                                                                                                                                                       Convert​ ​ ​ Convert x to a floating point number

complex(real [,imag ]) Create a complex number

str(x) Convert object x to a string

repr(x)                 Convert object x to expression string

eval(str )                                                                         Used to evaluate a valid Python expression in a string and return an object

tuple(s)                                                                Convert the sequence s into a tuple

list (S) Convert the sequence s with a list

# chr (x) to convert an integer to a character

## unichr (x) to convert an integer to unicode character

# ord(x )                                                                                           ‐ Convert a character to its integer value                                             ’’’’’’ out out through out''''''' ’ ’ ’’’’’’’’’’' ’ ’''''''''’’''''''‐‐' Convert's''''‐'''' through through''''''‐‐‐‐‐‐ involved in. to to d(x to)                                                    . ) Convert an integer to An octal string

Related recommendations: "

Python Video Tutorial

"

Convert a hexadecimal string to a decimal number:

>>> print int('ff', 16)   
255

To convert decimal numbers into hexadecimal strings, you can call the built-in hex() function:

>>> print hex(255)   
0xff

Call the b2a_hex() function in the BinAscii module to convert the ASCII-encoded text is expressed in hexadecimal:


>>> print binascii.b2a_hex('A')   
41

Conversely, you can also replace the hexadecimal-encoded text with ASCII-encoded text:


>>>print binascii.a2b_hex('41')
“A”

The above is the detailed content of How to convert string into hex in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn