uint转字符串 c语言,如何将uint32转换成无符号字符数组?

我试图在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"

也许问题是我怎样才能以八进制而不是十六进制打印字节?在

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值