您当前的位置 :环球传媒网>博客 > 正文
全球热推荐:16进制字符串如何转化为16进制值?
2022-07-13 15:42: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:");

}

}

}

关键词: 十六进制字符 16进制字符串如何转化为16进制值

相关阅读
分享到:
版权和免责申明

凡注有"环球传媒网"或电头为"环球传媒网"的稿件,均为环球传媒网独家版权所有,未经许可不得转载或镜像;授权转载必须注明来源为"环球传媒网",并保留"环球传媒网"的电头。