如何在python 3.7中将ascii字符串更改为十六进制,反之亦然?

时间:2018-09-01 13:58:32

标签: data-conversion python-3.7

我在此站点上找到了一些解决方案,但在python 3.7中不起作用。 所以,我问了一个新问题。

“ the”的十六进制字符串为“ 746865”

我想要一个将“ the”转换为“ 746865”并将“ 746865”转换为“ the”的解决方案

4 个答案:

答案 0 :(得分:1)

//emit array as a sequence of values
myArray = [1,2,3,4,5];
const arraySource = Rx.Observable.from(myArray);
//output: 1,2,3,4,5
const subscribe = arraySource.subscribe(val => console.log(val));

myArray.push(10);
myArray.push(11);



I want the output to include 10 and 11 too but the current output only 
shows 1,2,3,4,5

答案 1 :(得分:0)

鉴于您的字符串仅包含ascii(每个char的范围为0-0xff),您可以使用以下代码段:

In [28]: s = '746865'

In [29]: import math

In [30]: int(s, base=16).to_bytes(math.ceil(len(s) / 2), byteorder='big').decode('ascii')
Out[30]: 'the'

首先,您需要将字符串转换为以16为底的整数,然后将其转换为字节(假设每个字节2个字符),然后使用decode

将字节转换回字符串

答案 2 :(得分:0)

如果您有字节字符串,则:

>>> import binascii
>>> binascii.hexlify(b'the')
b'746865'

如果您有Unicode字符串,则可以对其进行编码:

>>> s = 'the'
>>> binascii.hexlify(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'
>>> binascii.hexlify(s.encode())
b'746865'

结果是一个字节字符串,您可以对其进行解码以获得Unicode字符串:

>>> binascii.hexlify(s.encode()).decode()
'746865'

相反,当然是:

>>> binascii.unhexlify(b'746865')
b'the'

答案 3 :(得分:0)

    #!/usr/bin/python3
    """
    Program name:  ASC_to_txt.py
    The program's input is a string of hexadecimal digits.
    The string is a bytes object, and each byte is supposed to be
    the hex ASCII-code of a (capital or small) letter.

    The program's output is the string of the corresponding letters.

    Example
    Input:            746865
    First subresult:  ['7','4','6','8','6','5']
    Second subresult: ['0x74', '0x68', '0x65']
    Third subresult:  [116, 104, 101]
    Final result:     the

    References
    Contribution by alhelal to stackoverflow.com (20180901)
    Contribution by QintenG to stackoverflow.com (20170104)
    Mark Pilgrim, Dive into Python 3, section 4.6
    """
    import string

    print("The program converts a string of hex ASCII-codes")
    print("into the corresponding string of letters.")
    print("Input range is [41, 42, ..., 5a] U [61, 62, ..., 7a]. \n")
    x = input("Input the hex ASCII-codes, eg. 746865: ")

    result_1 = []
    for i in range(0,len(x)//2):
        for j in range(0,2):
            result_1.extend(x[2*i+j])
            # First subresult

    lenres_1 = len(result_1)

    result_2 = []
    for i in range(0,len(result_1) - 1,2):
        temp = ""
        temp = temp + "0x" + result_1[i]      #0, 2, 4
        temp = temp +        result_1[i + 1]  #1, 3, 5
        result_2.append(temp)
        # Second subresult

    result_3 = []
    for i in range(0,len(result_2)):
        result_3.append(int(result_2[i],16))
        # Third subresult

    by = bytes(result_3)
    result_4 = by.decode('utf-8')
    # Final result

    print("Corresponding string of letters:" + " "*6, result_4, end = "\n")