简体   繁体   English

如何在 python 中从字符串转换为 16 位无符号 integer?

[英]How to convert from string to 16-bit unsigned integer in python?

I'm currently working on some encoding and decoding of the string in python.我目前正在对 python 中的字符串进行一些编码和解码。 I was supposed to convert some code from C# to python, however I encountered some problem as below:我应该将一些代码从 C# 转换为 python,但是我遇到了一些问题,如下所示:

So now I have a string that looks like this: 21-20-89-00-67-00-45-78 The code was supposed to eliminates the - in between the numbers, and packed 2 integers into 1 group, then convert them into bytes.所以现在我有一个看起来像这样的字符串: 21-20-89-00-67-00-45-78该代码应该消除数字之间的- ,并将2个整数打包成1组,然后将它们转换成字节。 In C#, it was done like this:在 C# 中,它是这样完成的:

var value = "21-20-89-00-67-00-45-78";
var valueNoDash = value.Replace("-", null);
for (var i = 0; i < DataSizeInByte; i++)
{
    //convert every 2 digits into 1 byte
    Data[i] = Convert.ToByte(valueNoDash.Substring(i * 2, 2), 16);
}

The above code represents Step 1: Remove - from the string, Step 2: using Substring method to divide them into 2 digits in 1 group, Step 3: use Convert.ToByte with base 16 to convert them into 16-bit unsigned integer.上面的代码表示步骤 1:从字符串中删除-步骤 2:使用 Substring 方法将它们分成 1 组中的 2 位,步骤 3:使用以 16 为底的 Convert.ToByte 将它们转换为 16 位无符号 integer。 The results in Data is Data中的结果是

33
32
137
0
103
0
69
120

So far I have no problem with this C# code, however when I try to do the same in python, I could not get to the same result as the C# code.到目前为止,我对这个 C# 代码没有任何问题,但是当我尝试在 python 中做同样的事情时,我无法得到与 C# 代码相同的结果。 My python code are as below:我的 python 代码如下:

from textwrap import wrap
import struct

values = "21-20-89-00-67-00-45-78"
values_no_dash = a.replace('-', '')
values_grouped = wrap(b, 2)
values_list = []

for value in values_grouped:
    values_list.append(struct.pack('i', int(value)))

In python, it gives me list of bytes in hex value, which is as below:在 python 中,它给了我十六进制值的字节列表,如下所示:

b'\x15\x00\x00\x00'
b'\x14\x00\x00\x00'
b'Y\x00\x00\x00'
b'\x00\x00\x00\x00'
b'C\x00\x00\x00'
b'\x00\x00\x00\x00'
b'-\x00\x00\x00'
b'N\x00\x00\x00'

This is in bytes object, however when I converted this object into Decimal, it gives me the exact same value as the original string: 21, 20, 89, 0, 67, 0, 45, 78 .这是字节 object 21, 20, 89, 0, 67, 0, 45, 78但是当我将此 object 转换为十进制时,它给了我与原始字符串完全相同的值:21、20、89、0、67、0、45、78。

Which means I did not convert successfully into 16-bit unsigned integer right?这意味着我没有成功转换为 16 位无符号 integer 对吗? How can I do this in python?如何在 python 中执行此操作? I've tried using str.encode() but the result still different.我试过使用str.encode()但结果仍然不同。 How can I achieve what C# had done in python?我怎样才能实现 C# 在 python 中所做的事情?

Thanks and appreciates if anyone can help!感谢并感谢任何人可以提供帮助!

I think this is the solution you're looking for:我认为这是您正在寻找的解决方案:

values = "21-20-89-00-67-00-45-78"
values_no_dash_grouped = values.split('-') #deletes dashes and groups numbers simultaneously 
for value in values_no_dash_grouped:
    print(int(value, 16)) #converts number in base 16 to base 10 and prints it

Hope it helps!希望能帮助到你!

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

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