gpt4 book ai didi

将长字符数组转换为字节数组

转载 作者:太空宇宙 更新时间:2023-11-04 04:07:03 25 4
gpt4 key购买 nike

如何将数字的字符数组转换为字节数组?示例:

char *digit="3224833640520308023"//long long array 

转换为:

uint8_t buff[256]= {0x2c, 0xc0, 0xe9, 0x1c, 0x32, 0xf1, 0x55, 0x37, 0};

(2c c0 e9 1c 32 f1 55 37)

最佳答案

最后我倒序打印了。如果您需要该字节序的数组,您可能需要字节序交换。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *digit="3224833640520308023";

int main() {
int i;
unsigned char byteArray[16];
unsigned long long x = strtoull(digit,0,10);

printf("%llx\n",x);
printf("%llu\n",x);
for (i=0;i<8;i++) {
byteArray[i] = (x>>(i*8)) & 0xFF;
}

printf ("Array is:\n");
for (i=7;i>=0;i--) {
printf("%2.2x ",byteArray[i]);
}
return 0;
}

关于将长字符数组转换为字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4807268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com