简体   繁体   English

Python:将二进制数据转换为十六进制数组

[英]Python: Converting binary data to hex array

I have a binary file that contains one line of: abcd1234我有一个二进制文件,其中包含一行:abcd1234

In Python, reading this binary file using在 Python 中,使用读取这个二进制文件

filecontent = f_obj.read()

results in:结果是:

filecontent = b'abcd1234'

I want to store the result of filecontent into a byte array as:我想要的结果存储filecontent成字节数组为:

array[0] = 0xab
array[1] = 0xcd
array[2] = 0x12
array[3] = 0x34

Is there a Python function that can do this conversion?是否有可以进行这种转换的 Python 函数?

filecontent = filecontent.decode("utf-8") # to remove the b' ' from the string
filecontent = bytearray.fromhex(filecontent)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM