十六进制字符串转换成uint8数组

将02012B1530A6E3958A98530031902003876940000000000CDF9844173BE512AFFFFFFE11DBBA1F00079387800E13012E11FC017FFFFFFFFE39C10F40
类型的字符串转化为
uint8_t array_uint[] = {0x02, 0x01, 0x2B, 0x15, 0x30, 0xA6, 0xE3, 0x95, 0x8A, 0x98, 0x53, 0x00, 0x31, 0x90, 0x20, 0x03, 0x87, 0x69, 0x40, 0x00, 0x00, 0x00, 0x00, 0x0C, 0xDF, 0x98, 0x44, 0x17, 0x3B, 0xE5, 0x12, 0xAF, 0xFF, 0xFF, 0xFE, 0x11, 0xDB, 0xBA, 0x1F, 0x00, 0x07, 0x93, 0x87, 0x80, 0x0E, 0x13, 0x01, 0x2E, 0x11, 0xFC, 0x01, 0x7F, 0xFF, 0xFF, 0xFF, 0xFE, 0x39, 0xC1, 0x0F, 0x40};

size_t convert_hex(uint8_t* dest, size_t count, const char* src)
{
    size_t i;
    int value;

    for (i = 0; i < count && sscanf(src + i * 2, "%2x", &value) == 1; i++) {
        dest[i] = value;
    }
    return i;
}
int main()
{
	const char* mychar = (char*)"01a2c3d4";
    uint8_t dest[20];
    // uint8_t* dest;//这种写法错误,没有分配地址,不知道往哪里存
    size_t cou = 4;//每两位转化一个,cou=sizeof(mychar)/2
    convert_hex(dest, cou, mychar);
    printf("%02x-%02x-%02x-", dest[0], dest[1], dest[2]);//01-a2-c3- 其中%02x表示以16进制输出,补齐两位,高位补0
    return 0;
}

你可能感兴趣的:(c语言应用,c语言,算法,数据结构)