简体   繁体   English

如何将uint32_t转换为unsigned char数组?

[英]How to convert uint32_t to unsigned char array?

I'm trying to replicate conversion uint32_t values to unsigned char arrays in python (I've already done it in C) 我正在尝试将转换uint32_t值复制到python中的unsigned char数组(我已经在C中完成了它)

This is my existing C function: 这是我现有的C函数:

unsigned char *uint32_to_char_array(const uint32_t n)
{
    unsigned char *a;

    a = wrap_calloc(4, sizeof(unsigned char));

    a[0] = (n >> 24) & 0xff;  /* high-order (leftmost) byte: bits 24-31 */
    a[1] = (n >> 16) & 0xff;  /* next byte, counting from left: bits 16-23 */
    a[2] = (n >>  8) & 0xff;  /* next byte, bits 8-15 */
    a[3] = n         & 0xff;  /* low-order byte: bits 0-7 */

    return a;
}

If I were to do the following in gdb: 如果我在gdb中执行以下操作:

(gdb) p uint32_to_char_array(0x00240918)[0]@4  = "\000$\t\030"

And it's that string I'm trying to generate in python. 这就是我想在python中生成的字符串。

ie for a uint32_t input value of 0x240918 I want an output string of "\\000$\\t\\030" 即对于uint32_t输入值0x240918我想要一个输出字符串"\\000$\\t\\030"

I've scoured SO but to no avail thus far, particularly this -> How to convert integer value to array of four bytes in python but none of the answers seem to yield the input/output combination stated above 我已经搜索过SO但到目前为止无济于事,特别是这个 - > 如何在python中将整数值转换为四个字节的数组但是没有一个答案似乎产生上面提到的输入/输出组合

I'm using 2.7, but could use > 3.0 if required. 我使用2.7,但如果需要可以使用> 3.0。

Update: 更新:

Python 3.5.2 (default, Nov 12 2018, 13:43:14) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 0x240918.to_bytes(4, "big")
b'\x00$\t\x18'

Hmmm a bit different — I'm sure the answer is staring me in the face here but I can't see what it is? 嗯有点不同 - 我确定答案是在这里盯着我,但我看不出它是什么?

So I can see: 所以我可以看到:

>>> b"\000$\t\030"
b'\x00$\t\x18'

But how might one achieve the reverse? 但是如何才能实现相反的目标呢? ie

>>> b'\x00$\t\x18'
b"\000$\t\030"

Maybe the question is how I can print a bytes-literal in octal rather than hexadecimal? 也许问题是我如何以八进制而不是十六进制打印字节文字?

hmmm a bit different - i'm sure the answer is staring me in the face here but can't see what it is? 嗯有点不同 - 我确定答案是在这里盯着我,但看不出它是什么?

30 octal ie "\\030" is the same as 18 hexadecimal ie "\\x18" . 30八进制,即"\\030"与18十六进制相同,即"\\x18" Both of them represent a single byte in your byte sequence with a decimal value of 24. 它们都代表字节序列中的单个字节,十进制值为24。

You can compare the exact values in the REPL: 您可以比较REPL中的确切值:

bytes((0x00240918 >> i & 0xff) for i in (24,16,8,0)) == b"\000$\t\030"
True

Check the Python documentation on string and byte literals : 查看有关字符串和字节文字的Python文档

  • \\ooo Character with octal value ooo \\ooo八进制值ooo的字符
  • \\xhh Character with hex value hh \\xhh十六进制值hh的字符

These can be used in byte literals as well as strings (keep in mind that strings are byte sequences in Python 2). 这些可以用在字节文字和字符串中(请记住,字符串是Python 2中的字节序列)。

I don't think bytes support an octal representation by default (the ascii codec always uses hex), but you can code your own: 我不认为bytes默认支持八进制表示(ascii编解码器总是使用十六进制),但你可以自己编写代码:

import re
my_b = b'\x00$\t\x18'
print(re.sub(r'\\x([0-9a-f]{2})', lambda a: "\\%03o" % int(a.groups()[0], 16),
  str(my_b)))
# Console result: b'\000$\t\030'

Keep in mind that the string contains verbatim quotes and the b' prefix, and it may accept escaped slashes as a hex sequence. 请记住,该字符串包含逐字引号和b'前缀,并且它可以接受转义斜杠作为十六进制序列。 If you really want a good octal __repr__ the best way would be to create a loop and check for non-printable characters, convert them to 3 digit octal and join everything into a string. 如果你真的想要一个好的八进制__repr__ ,最好的方法是创建一个循环并检查不可打印的字符,将它们转换为3位八进制并将所有内容连接成一个字符串。

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

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