我试图在python中将转换uint32_t值复制到unsigned char数组(我已经用C语言完成了)
这是我现有的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;
}
如果我要在gdb中执行以下操作:
^{pr2}$
我正试图用python生成这个字符串。在
例如,对于uint32_t的输入值0x240918,我想要一个输出字符串"\000$\t\030"
我使用的是2.7版,但如果需要,可以使用3.0版。在
更新: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'
嗯,有点不同-我肯定答案就在眼前,但我看不出来是什么?在
所以我可以看到:>>> b"\000$\t\030"
b'\x00$\t\x18'
但是怎样才能达到相反的效果呢?i、 e>>> b'\x00$\t\x18'
b"\000$\t\030"
也许问题是我怎样才能以八进制而不是十六进制打印字节?在