C语言快速互转HEX(16进制)和原始字符串/数组

hex转数值

缘由

这个起因是昨晚群里有人在讨论怎么把字符串转成HEX方法最佳,讨论到最后变成哪种方法效率最优了。毕竟这代码是要在MCU上面跑的,要同时考虑到时间和空间的最优解。

当然讨论的是有结果的,具体实现的方法和代码在下面展示。

char数组转16进制HEX串

例子:

将如下的量

char str[] = "12345";
char data[] = {1,2,3,4,5,0xff};

转成

"313233343500"
"0102030405FF"

这样的结果

这个其实很简单,追求速度的话,查表就好了

0-16对应0-F即可:

const char hex_table[] = {
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
};

然后一个个从表里取出来,拼到对应位置即可:

void to_hex(char *s, int l, char *d)
{
    while(l--)
    {
        *(d+2*l+1) = hex_table[(*(s+l))&0x0f];
        *(d+2*l) = hex_table[(*(s+l))>>4];
    }
}

完整测试代码如下:

#include <stdio.h>
const char hex_table[] = {
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
};
void to_hex(char *s, int l, char *d)
{
    while(l--)
    {
        *(d+2*l+1) = hex_table[(*(s+l))&0x0f];
        *(d+2*l) = hex_table[(*(s+l))>>4];
    }
}
int main () {
    char s[]= "1234";
    char d[9];
    d[8] = '\0';
    to_hex(s,4,d);
    printf("%s",d);
    return 0;
}

输出结果:31323334

16进制HEX串转成数值数组

例子:

将类似"AAbb2fFF"的量转成{0xAA,0xBB,0x2F,0xff}这样的结果

这里如果还用查表的话,这个rom占用会浪费掉不少空间,所以查表法直接就被否决掉了(如果是PC上,追求极致速度的话,当然可以用)。

同时,为了通用性,代码需要兼容大小写两种输入数据

在仔细研究数据的结构时,我发现了个规律:

ASCII中的0-9对应了0x30-0x39
ASCII中的A-F对应了0x41-0x46
ASCII中的a-f对应了0x61-0x66

也就是说,只要这一个字符大于0x39,那它一定是字母;同时,在上面的分析也可以发现,如果这个字符是字母,不论大写小写,只需要看低四位就可以直接判断这个字符代表的数是多少

具体逻辑如下:

判断这个字符是否大于0x39

如果不是,直接取这个字符的低四位当作结果

如果是,则为字母,将他的低四位加上9即为所需结果

具体实现代码也如下:

void from_hex(char *s, int l, char *d)
{
    while(l--)
    {
        char* p = s+l;
        char* p2 = p-1;
        *(d+l/2) =
        ( (*p>'9'? *p+9 : *p) & 0x0f ) |
        ( (*p2>'9'? *p2+9 : *p2) << 4 );
        l--;
    }
}

完整的测试代码:

#include <stdio.h>

void from_hex(char *s, int l, char *d)
{
    while(l--)
    {
        char* p = s+l;
        char* p2 = p-1;
        *(d+l/2) =
        ( (*p>'9'? *p+9 : *p) & 0x0f ) |
        ( (*p2>'9'? *p2+9 : *p2) << 4 );
        l--;
    }
}

int main () {
    char s[]= "6F6B6f6b";
    char d[5];
    d[4] = '\0';
    from_hex(s,8,d);
    printf("%s",d);
    return 0;
}

输出结果:okok

EOF

如果你有更好的方法,欢迎在下面留言讨论😁

更新

2020.3.9:Antecer带来了更高效的hex转数组代码

#include <stdio.h>

void from_hex(char *s, int l, char *d)
{
    while(l--)
    {
        *d = (*s>'9' ? *s+9 : *s) << 4;
        ++s;
        *d |= (*s>'9' ? *s+9 : *s) & 0x0F;
        ++s;
        ++d;
    }
}

int main () {
    char s[]= "6F6B6f6b";
    char d[5];
    d[4] = '\0';
    from_hex(s,4,d);
    printf("%s",d);
    return 0;
}

因为传入的指针是临时变量,所以数组转hex也可以按此思路稍微优化下:

#include <stdio.h>
const char hex_table[] = {
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
};
void to_hex(char *s, int l, char *d)
{
    while(l--)
    {
        *d = hex_table[*s >> 4];
        d++;
        *d = hex_table[*s & 0x0f];
        s++;
        d++;
    }
}
int main () {
    char s[]= "1234";
    char d[9];
    d[8] = '\0';
    to_hex(s,4,d);
    printf("%s",d);
    return 0;
}

2020.3.10:稀饭放姜发现内嵌“++”操作比单独写一行运行要快

hex转数组:

#include <stdio.h>

void from_hex(char *s, int l, char *d)
{
    while(l--)
    {
        *(d++) = ( (*s>'9' ? *(s++)+9 : *(s++)) << 4 )
        | ( (*s>'9' ? *(s++)+9 : *(s++)) & 0x0F );
    }
}

int main () {
    char s[]= "6F6B6f6b";
    char d[5];
    d[4] = '\0';
    from_hex(s,4,d);
    printf("%s",d);
    return 0;
}

数组转hex:

#include <stdio.h>
const char hex_table[] = {
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
};
void to_hex(char *s, int l, char *d)
{
    while(l--)
    {
        *(d++) = hex_table[*s >> 4];
        *(d++) = hex_table[*(s++) & 0x0f];
    }
}
int main () {
    char s[]= "1234";
    char d[9];
    d[8] = '\0';
    to_hex(s,4,d);
    printf("%s",d);
    return 0;
}

9 Comments

  1. Google Chrome 80.0.3987.132 Google Chrome 80.0.3987.132 Windows 10 x64 Edition Windows 10 x64 Edition

    我觉得这样效率会高点?大概。(这里的l表示输出字符的个数)

    void hex_to_str(char* s, char* d, int l)
    {
        while(l--)
        {
            *d = (*s & 0x40 ? *s + 9 : *s) << 4;
            ++s;
            *d |= (*s & 0x40 ? *s + 9 : *s) & 0xF;
            ++s;
            ++d;
        }
    }
    
  2. Google Chrome 94.0.4606.71 Google Chrome 94.0.4606.71 Mac OS X  10.15.7 Mac OS X 10.15.7

    to_hex方法,我测试有个问题,应该改成这样:
    (d + 2 * l) = hex_table[(((s + l)) & 0xF0) >> 4];

发表评论

您的电子邮箱地址不会被公开。