十六进制字符串到C中的字节数组

是否有标准的C函数将十六进制字符串转换为字节数组
我不想编写自己的函数。

慕尼黑的夜晚无繁华
浏览 535回答 3
3回答

烙印99

据我所知,尚无标准功能,但可以通过以下方式轻松实现:#include <stdio.h>int main(int argc, char **argv) {&nbsp; &nbsp; const char hexstring[] = "DEadbeef10203040b00b1e50", *pos = hexstring;&nbsp; &nbsp; unsigned char val[12];&nbsp; &nbsp; &nbsp;/* WARNING: no sanitization or error-checking whatsoever */&nbsp; &nbsp; for (size_t count = 0; count < sizeof val/sizeof *val; count++) {&nbsp; &nbsp; &nbsp; &nbsp; sscanf(pos, "%2hhx", &val[count]);&nbsp; &nbsp; &nbsp; &nbsp; pos += 2;&nbsp; &nbsp; }&nbsp; &nbsp; printf("0x");&nbsp; &nbsp; for(size_t count = 0; count < sizeof val/sizeof *val; count++)&nbsp; &nbsp; &nbsp; &nbsp; printf("%02x", val[count]);&nbsp; &nbsp; printf("\n");&nbsp; &nbsp; return 0;}编辑正如Al所指出的那样,如果字符串中的十六进制数字为奇数,则必须确保以0开头。例如, 上面的示例"f00f5"将字符串{0xf0, 0x0f, 0x05}错误地评估为字符串,而不是正确的字符串。{0x0f, 0x00, 0xf5}。稍微修改了示例以解决@MassimoCallegari的评论

LEATH

我通过谷歌搜索发现了同样的问题。我不喜欢调用sscanf()或strtol()的想法,因为它感觉像是过分杀了。我编写了一个快速函数,该函数不能验证文本确实是字节流的十六进制表示形式,但是可以处理奇数个十六进制数字:uint8_t tallymarker_hextobin(const char * str, uint8_t * bytes, size_t blen){&nbsp; &nbsp;uint8_t&nbsp; pos;&nbsp; &nbsp;uint8_t&nbsp; idx0;&nbsp; &nbsp;uint8_t&nbsp; idx1;&nbsp; &nbsp;// mapping of ASCII characters to hex values&nbsp; &nbsp;const uint8_t hashmap[] =&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //&nbsp; !"#$%&'&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ()*+,-./&nbsp; &nbsp; &nbsp;0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // 01234567&nbsp; &nbsp; &nbsp;0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 89:;<=>?&nbsp; &nbsp; &nbsp;0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, // @ABCDEFG&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // HIJKLMNO&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // PQRSTUVW&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // XYZ[\]^_&nbsp; &nbsp; &nbsp;0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, // `abcdefg&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // hijklmno&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // pqrstuvw&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // xyz{|}~.&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........&nbsp; &nbsp; &nbsp;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00&nbsp; // ........&nbsp; &nbsp;};&nbsp; &nbsp;bzero(bytes, blen);&nbsp; &nbsp;for (pos = 0; ((pos < (blen*2)) && (pos < strlen(str))); pos += 2)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; idx0 = (uint8_t)str[pos+0];&nbsp; &nbsp; &nbsp; idx1 = (uint8_t)str[pos+1];&nbsp; &nbsp; &nbsp; bytes[pos/2] = (uint8_t)(hashmap[idx0] << 4) | hashmap[idx1];&nbsp; &nbsp;};&nbsp; &nbsp;return(0);}

料青山看我应如是

对于短字符串,strtol,strtoll,并且strtoimax将工作得很好(请注意,第三个参数是基地在使用处理字符串...它设置为16)。如果您的输入时间长于此,number-of-bits-in-the-longest-integer-type/4那么您将需要其他答案建议的一种更灵活的方法。
打开App,查看更多内容
随时随地看视频慕课网APP