简体   繁体   English

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

[英]convert bytes into hexadecimal in python

I am trying to multiply two data of bytes data type using python by trying to converting them into hexadecimal values.我试图通过尝试将它们转换为十六进制值来使用 python 将字节数据类型的两个数据相乘。 but it seems that the hex() function is not working.但似乎 hex() function 不起作用。 Can you please help me on that.你能帮我解决这个问题吗? Below is my code (x and y are our data of type bytes)下面是我的代码(x 和 y 是我们的字节类型数据)

data=bytes.hex(x) * bytes.hex(y)

I am getting the error: TypeError:'str' object cannot be interpreted as an integer我收到错误:TypeError:'str' object 不能被解释为 integer

by trying通过尝试

data = hex(x) * hex(y)

the error become: TypeError:'bytes' object cannot be interpreted as an integer错误变为: TypeError:'bytes' object 不能被解释为 integer

Can anyone help please?有人可以帮忙吗?

hex(my_var) returns a string. hex(my_var)返回一个字符串。 To do math you need to convert your variables to int:要进行数学运算,您需要将变量转换为 int:

x_int = int(x, base=16)
y_int = int(y, base=16)

decimal_value = x_int * y_int # integer
hex_value = hex(decimal_value) # string

data = hex(x) * hex(y)

This doesn't work because the hex() function expects an int as an argument and outputs a string.这不起作用,因为hex() function 需要一个 int 作为参数并输出一个字符串。

In Python 3, you can do the following (interprets bytes as base 10)在 Python 3 中,您可以执行以下操作(将字节解释为基数 10)

data = hex(int(x)*int(y))

I see in the comments below your question that you have examples of your x and y values - you have some very large numbers to process if you interpret the bytes objects as arbitrary length integers.我在您的问题下方的评论中看到您有 x 和 y 值的示例 - 如果您将字节对象解释为任意长度的整数,您将需要处理一些非常大的数字。 Not sure this is what you want, but this will do conversion of bytes to/from integer values.不确定这是您想要的,但这会在 integer 值之间进行字节转换。

>>> int_x = int.from_bytes(x, 'big')              # From bytes to int.
>>> int_y = int.from_bytes(y, 'big')
>>> int_z = int_x * int_y                         # int_z as an integer.
>>>
>>> # Converting int_z to a bytes object:
>>>
>>> bytes_length = (int_z.bit_length() + 7) // 8
>>>
>>> z = int_z.to_bytes(bytes_length, 'big')

If you want them interpreted as little endian, replace 'big' with 'little'.如果您希望它们被解释为小端,请将“大”替换为“小”。

If your y value is already an int type, which it looks like in your comment, then don't convert it before multiplying:如果您的y值已经是 int 类型,它在您的评论中看起来像,那么在相乘之前不要转换它:

>>> int_x = int.from_bytes(x, 'big')
>>> int_y = y
>>> int_z = int_x * int_y
>>> z     = int_z.to_bytes(bytes_length, 'big')

The first parameter to int.to_bytes() is the length of the bytes object to create. int.to_bytes()的第一个参数是要创建的bytes object 的长度。 There are other ways to calculate this value, but the way I included is the fastest method out of 3 that I timed.还有其他方法可以计算此值,但我所包含的方法是我计时的 3 种方法中最快的方法。

If all you wanted to do was convert a bytes object to its hex string representation ( z below is treated as a bytes object):如果您只想将bytes object 转换为其十六进制字符串表示形式(下面的z被视为bytes对象):

>>> hex_z = '0x' + ''.join(hex(b)[2:] for b in z)

Or to convert an int to a hex string:或者将int转换为十六进制字符串:

>>> hex_z = hex(int_z)

hex() can handle very large integers. hex()可以处理非常大的整数。

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

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