在python中将字节字符串转换为十六进制

2024-10-01 02:22:44 发布

您现在位置:Python中文网/ 问答频道 /正文

给定此文件(utf8):

00000000  30 31 3a 32 35 20 e2 87  92 20 31 32 2f 32 34 2f  |01:25 ... 12/24/|
00000010  32 30 31 39 20 e2 87 89  30 31 3a 33 31 20 44 49  |2019 ...01:31 DI|
00000020  53 4b 20 46 20 e2 99 a3  20 0d 0a                 |SK F ... ..|

我的目的是获取一个文件或字节字符串,并将其转换为十六进制表示。 我创建了以下代码:

def c2h(data):
    def c2h(c):
        hb = hex(ord(c)).replace('0x','')
        return hb if len(hb) == 2 else ''.join('0'+hb)
    strbuf = []
    i = 0
    for c in data:
        if ord(c) > 255:
        raise ValueError("ord value of char @ position:{:2d} is > 255".format(i))
        strbuf.append(c2h(c))
        i += 1
    return ''.join(strbuf)

然后我把上面的代码放在Mac、Windows和Linux上运行。以下是结果

Mac:Python 2.7.16版

>>> file = '/Volumes/TEMP/KDACCT.TXT'
>>> f = open(file, 'r')
>>> s1 = f.read().rstrip('\r\n')
>>> s1
'01:25 \xe2\x87\x92 12/24/2019 \xe2\x87\x8901:31 DISK F \xe2\x99\xa3 '
>>> c2h(s1)
'30313a323520e287922031322f32342f3230313920e2878930313a3331204449534b204620e299a320'

我得到了我所期望的,但是如果我在Windows或Linux中使用相同的文件,我会得到一个ValueError

以下是Windows交互: Windows:Python 3.6.8

>>> file = 'c:\\temp\\kdacct.txt'
>>> f = open(file, 'r')
>>> s1 = f.read().rstrip('\r\n')
>>> s1
'01:25 ⇒ 12/24/2019 ⇉01:31 DISK F ♣ '
>>> c2h(s1)
I get ValueError: ord value of char @ position:10 is > 255

请注意,Windows存储BOM表

以下是Linux交互: Linux:Python 3.6.8版

>>> file = '/media/sf_Linux_SHR/KDACCT.TXT'
>>> f = open(file, 'r')
>>> s1 = f.read().rstrip('\r\n')
>>> s1
'01:25 ⇒ 12/24/2019 ⇉01:31 DISK F ♣ '
>>> c2h(s1)
I get ValueError: ord value of char @ position: 6 is > 255

我的问题是如何在Windows/Linux中获得与在Mac上相同的结果。 我想这和编码有关,我只是不知道需要做什么


Tags: 文件ofvaluelinuxwindowspositionfilevalueerror
1条回答
网友
1楼 · 发布于 2024-10-01 02:22:44

这里有几个问题

  1. 您正在将二进制文件作为ascii文件读取。应该使用rb而不是r作为open()的参数
  2. 您混合了python2和python3,它们在本例中的行为有些不同

以下是一个适用于这两种情况的版本:

from __future__ import print_function


with open('/Volumes/TEMP/KDACCT.TXT', 'rb') as fh:
    characters = bytearray(fh.read())
    for character in characters:
        print('%02x' % character, end='')

相关问题 更多 >