gpt4 book ai didi

将字符串从 argv 转换为无符号字节数组

转载 作者:太空宇宙 更新时间:2023-11-04 04:14:01 24 4
gpt4 key购买 nike

例如我需要一个

unsigned code[] {
0x0001,
0x0002,
0x0003,
.. and so on
}

来自 argv。我想将它作为这样的流输入:

000100020003 and so on

最佳答案

unsigned hextodigit(int hex)
{
unsigned result = 0;

switch(toupper(hex))
{
case '0':
return 0;
break;
case '1':
return 1;
break;
case '2':
return 2;
break;
case '3':
return 3;
break;
case '4':
return 4;
break;
case '5':
return 5;
break;
case '6':
return 6;
break;
case '7':
return 7;
break;
case '8':
return 8;
break;
case '9':
return 9;
break;
case 'A':
return 10;
break;
case 'B':
return 11;
break;
case 'C':
return 12;
break;
case 'D':
return 13;
break;
case 'E':
return 14;
break;
case 'F':
return 15;
break;
default:
break;
}
}


unsigned fromhex4chars(const char *str)
{
return hextodigit(*(str + 3)) + (hextodigit(*(str + 2)) << 4) + (hextodigit(*(str + 1)) << 8) + (hextodigit(*str) << 12);
}

void convert(const char *str, unsigned *buff)
{
size_t len = strlen(str) / 4;

while(len--)
{
*buff++ = fromhex4chars(str);
str += 4;
}
}

int main(int argc, char **argv)
{
unsigned x[100];

convert(argv[1], x);


return 0;
}

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

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