为了方便实际应用做了个16进制的字符串转16进制的数组,代码如下:,我来为大家科普一下关于字符串怎么转换为16进制数据?下面希望有你要的答案,我们一起来看看吧!

字符串怎么转换为16进制数据(16进制字符串如何转化为16进制值)

字符串怎么转换为16进制数据

为了方便实际应用做了个16进制的字符串转16进制的数组,代码如下:

void net_data_depack_from_hex_string(char *p_data_hex_char)

{

charbuf_str[1024];

UINT16buf_str_len = 0;

UINT16i;

UINT16count = 0;

for (i = 0; p_data_hex_char[i] != '\0'; i )

{

if (p_data_hex_char[i] >= 'a' && p_data_hex_char[i] <= 'f')

{

if (0x01 == (count & 0x01))

{

buf_str[buf_str_len] = (buf_str[buf_str_len] << 4) | (p_data_hex_char[i] - 0x57);

buf_str_len ;

}

else

{

buf_str[buf_str_len] = p_data_hex_char[i] - 0x57;

}

count ;

}

else if (p_data_hex_char[i] >= 'A' && p_data_hex_char[i] <= 'F')

{

if (0x01 == (count & 0x01))

{

buf_str[buf_str_len] = (buf_str[buf_str_len] << 4) | (p_data_hex_char[i] - 0x37);

buf_str_len ;

}

else

{

buf_str[buf_str_len] = p_data_hex_char[i] - 0x37;

}

count ;

}

else if (p_data_hex_char[i] >= '0' && p_data_hex_char[i] <= '9')

{

if (0x01 == (count & 0x01))

{

buf_str[buf_str_len] = (buf_str[buf_str_len] << 4) | (p_data_hex_char[i] - '0');

buf_str_len ;

}

else

{

buf_str[buf_str_len] = p_data_hex_char[i] - '0';

}

count ;

}

}

LOG_INFO("buf_str_len=%u,", buf_str_len);

cz_hex_dump((UINT8 *)buf_str, (INT32)buf_str_len, "buf_str");

{

UDZ_Head_tudz_head;

UINT8data_buf[WAN_PACK_MAX_LEN];

UINT16data_len = 0;

if (0 == net_data_depack((Net_Pack_t*)buf_str, &udz_head, data_buf, &data_len))

{

LOG_INFO("depack success,datalen=%u,", data_len);

cz_hex_dump((UINT8 *)&udz_head, sizeof(udz_head), "depack_udz_head content:");

cz_hex_dump(data_buf, data_len, "depack content:");

}

else

{

LOG_ERROR("fail:net data depack,buf_str_len=%u,", buf_str_len);

cz_hex_dump((UINT8 *)buf_str, buf_str_len, "depack content:");

}

}

}

,