Python implements method analysis to convert hexadecimal strings to ascii characters

  • 2020-06-12 09:55:19
  • OfStack

This article illustrates the Python implementation of converting hexadecimal strings to ascii characters. To share for your reference, specific as follows:

456 e633064316e675f31735f66336e string, through Python, according to the two characters, such as 45, 6 e, 63 form into ascii code format, output acsii code format string.

The code is as follows:

a = "456e633064316e675f31735f66336e"
''.join([chr(int(b, 16)) for b in [a[i:i+2] for i in range(0, len(a), 2)]])

Split the code, actually run 1, explain the meaning of 1 code:


[a[i:i+2] for i in range(0, len(a), 2)]

This is the lamda expression, which, in plain English, is an for loop, ranging from 0 to the length of the a string, each step is 2, so the first step is 0, the second step is 2, the third step is 4...

The corresponding a[i:i+2] is the first time to extract 0:0+2 from a, which is 0:2. So it is actually like an array, which is to extract the first two characters of the a string. Note that 0:2 here does not actually contain 2, only 0 and 1.

Then, the outer layer [] means to put the return result of the loop into a list, which is a comma-separated string containing []. But this expression can help simplify the code.

The actual result:

======================== C:\c -  A copy of the  (2).py ========================
['45', '6e', '63', '30', '64', '31', '6e', '67', '5f', '31', '73', '5f', '66', '33', '6e']


[chr(int(b, 16)) for b in [a[i:i+2] for i in range(0, len(a), 2)]]

This outer for loop is the result of the for loop to memory:

['45', '6e', '63', '30', '64', '31', '6e', '67', '5f', '31', '73', '5f', '66', '33', '6e']

I'm going through it again,1 element at a time, b in this case, and I'm going to convert b, where int(b,16) means that b is a hexadecimal number, and I'm going to convert that number to an int integer, and then the chr function converts that integer to a character, which is ascii.

The result is:

['E', 'n', 'c', '0', 'd', '1', 'n', 'g', '_', '1', 's', '_', 'f', '3', 'n']


''.join([chr(int(b, 16)) for b in [a[i:i+2] for i in range(0, len(a), 2)]])

Is to use ' 'behind the empty string, the [' E', 'n', 'c', '0', 'd', '1', 'n', 'g', '_', '1', 's', '_', 'f', '3', 'n'] in the list of each element, join series in 1 case.

The final result is: Enc0d1ng_1s_f3n

4. Another way to write it

a = "456e633064316e675f31735f66336e"
al = []
for i in range(0, len(a), 2):
  b = a[i:i+2]
  al.append(chr(int(b, 16)))
print ''.join(al)

PS: Here are some more tools for your reference:

ASCII Code Comparison Table:

Native/ASCII Online code conversion tool:

Online Chinese characters /ASCII code /Unicode code conversion tool:

Online any base conversion tool:

Online RGB color value and base 106 color code conversion tool:

For more information about Python, please refer to Python string Manipulation Skills summary, Python coding skills Summary, Python Data Structure and Algorithm Tutorial, Python Functions Summary and Python Introduction and Advanced Classic Tutorial.

I hope this article has been helpful in Python programming.

Related articles: