C++ hexadecimal string to hexadecimal array function

@TOCC++ 16进制字符串转16进制数组函数
unsigned char buf[], int len)
{
if (str != NULL && buf != NULL && len != NULL) {
int Length = sizeof(str);
if (Length % 2 == 0) {
int i = 0;
int n = 0;
while (*str != 0 && (n = ((i++) >> 1)) <len) {
buf[n] <<= 4;
if (*str >= ‘0’ && *str <= ‘9’) {
buf[n] |= *str - ‘0’;
} else if (*str >= ‘a’ && *str <= ‘f’) {
buf[n] |= *str - ‘a’ + 10;
} else if (*str >= ‘A’ && *str <= ‘F’) {
buf[n] |= *str - ‘A’ + 10;
}
str++;
}
len = n;
}
}
}

int main ()
{

char str[] = "453456ABCDFF";
unsigned char ret[6];
StrToHex(str, ret, 6);
for (int i = 0; i < 6; i++)
    {
        printf("%02X", ret[i]);
        printf("\n");
    }

return 0;
}
Insert picture description here

Guess you like

Origin blog.csdn.net/hs977986979/article/details/92408162