求一份C语言,const char* 转uint8_t数组方法
已知数据:const char* keyStr = "7E360165C72B";
想转换成如下数据
uint8_t keya = { 0x7E, 0x36, 0x01, 0x65, 0xC7, 0x2B }; 百度搜索 C hex 转数组 循环取2个字符用strtol转换 循环sscanf也可以实现 记得做好容错 楼上正解, 这种问题自己搜一下更好
/**
* 字符串转字节数组
* @Param source
*字符串
* @param length
*字符串长度
* @param target
*字节数组
* @param n
*转换后长度
*/
void hex_2_uint8(const char* source, size_t length, uint8_t* target, size_t n) {
n = 0;
for (int i = 0; i < length; i += 2) {
if (source >= 'a' && source <= 'f') {
target = static_cast<unsigned char>(source - 'a' + 10);
}
else if (source >= 'A' && source <= 'F') {
target = static_cast<unsigned char>(source - 'A' + 10);
}
else {
target = static_cast<unsigned char>(source - '0');
}
if (source >= 'a' && source <= 'f') {
target = static_cast<unsigned char>((target << 4) | (source - 'a' + 10));
}
else if (source >= 'A' && source <= 'F') {
target = static_cast<unsigned char>((target << 4) | (source - 'A' + 10));
}
else {
target = (target << 4) | (source - '0');
}
++n;
}
}
int main() {
const char* keyStr = "7E360165C72B";
uint8_t keya = { 0x7E, 0x36, 0x01, 0x65, 0xC7, 0x2B };
uint8_t test;
hex_2_uint8(keyStr, 12, test, 6);
} baidu搜索,很多的。 int string_to_hex(char *string, uint8_t *hex)
{
char *p;
int i;
uint8_t tmp;
for (i = 1, p = string; *p != '\0'; p++, i++)
{
if (*p > '9')
{
tmp = *p - 55;
}
else
{
tmp = *p - '0';
}
printf("tmp = %#x\n", tmp);
if (!(i % 2))
{
*hex |= tmp;
hex++;
}
else
{
*hex = tmp << 4;
}
}
return 0;
} #include <stdio.h>
#include <stdint.h>
#define ArraySize 6
int stringToUint8(const char* str, uint8_t* array, int arraySize);
int main(void)
{
const char* keyStr = "7E360165C72B";
uint8_t keya = {0};
int arrayValidLen=stringToUint8(keyStr, keya, ArraySize);
for (int i = 0; i < arrayValidLen; i++)
printf("%#x\t", keya);
return 0;
}
int stringToUint8(const char* str, uint8_t* array, int arraySize)
{
int arrayValidLen = 0;
for (int arrayElement = 0; arrayValidLen < arraySize && str != '\0'; arrayValidLen++)
{
sscanf(&str, "%2x", &arrayElement);
array = arrayElement;
}
return arrayValidLen;
} 我也发一个,也是以前网上找的,先把字符串整体转换成数字,然后通过位运算得到各个分量,可能数字太大会溢出,可以把函数改良成64位的
static UCHAR a2xtable[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0,
0,10,11,12,13,14,15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,10,11,12,13,14,15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
UINT util_atox(const char* s) {
UINT res = 0;
while (*s) {
res = (res << 4) | a2xtable[*s++];//利用二进制和十六进制之间的换算方法
}
return res;
}
页:
[1]