【问题标题】:c char array to uint8_t arrayc char 数组到 uint8_t 数组
【发布时间】:2018-05-28 14:08:08
【问题描述】:

我有一个关于我遇到的 c 问题的基本问题。我的输入字符数组会是这样的:

'DABC95C1' 

我想用它制作一个 uint8_t 数组

0xDA 0xBC 0x95 0xC1

我可以轻松访问每个字符,但我不知道如何形成0xDA。 c中有函数还是我可以直接转换它?

【问题讨论】:

  • strtoull 然后获取结果的字节数。
  • 您尝试了哪些打印方式,使用哪种 printf 格式说明符?结果如何?
  • 'char array' .... 你可以用" 用更清晰的 C 语言编写 sn-ps
  • 我可能会重复调用sscanf,格式为%2hhx

标签: c char uint8t


【解决方案1】:

使用strtoull 函数将字符串转换为给定基数的数字。然后只需移出所需的字节。如:

#include <stdio.h>
#include <stdlib.h>

int main(void) {

    unsigned long long res = strtoull("DABC95C1", NULL, 16);

    printf("%hhx, %hhx, %hhx, %hhx",
        (unsigned char)res,
        (unsigned char)((res >> 8)   &  0xFF),
        (unsigned char)((res >> 16)  &  0xFF),
        (unsigned char)((res >> 24)  &  0xFF)
    );

    return 0;
}

结果:

c1, 95, bc, da

Demo

注意事项:

由于您的要求是获取一个字节数组,您可能会想要做类似

的事情
uint8_t *arr = (uint8_t*)&res;

但这里有两个警告:

1) 我违反了strict aliasing rule(您可以通过将uint8_t 替换为char 以某种方式解决它)
2)返回字节的顺序将是特定于实现的(依赖于字节序),因此不可移植。另请注意,结果为unsigned long long,因此您可能会在数组的开头或结尾获得额外的填充零。

【讨论】:

  • 好伤心。我正要投票...但是,现在,我有疑问...(这不是 100% 完美 - 你知道。)
  • @Scheff OP 也想要一个数组,而不仅仅是打印。钓鱼竿,你懂的……
  • 假设uint8_t 是字符类型是相当安全的,因为如果给定系统不存在8 位字符类型,那么uint8_t 也将不存在。这仅适用于异国情调、过时的 DSP 等,没有人需要代码兼容性。
  • @Lundin 是的,坦率地说,我们正在我工作的地方做这件事。但我想它更像是匹配之前设计的代码,因为我是从头开始做的,所以我会尽可能地接近标准。
  • 字节数非常有限。最多 8 个。如果字符串长度超过 16 字节怎么办?
【解决方案2】:

按所选顺序的任何大小字符串。便携式数字转换,它在 ASCII 系统上优化得非常好。 https://godbolt.org/g/Ycah1e

#include <stdio.h>
#include <stdint.h>
#include <string.h>

int CharToDigit(const char c);
void *StringToTable(const char *str, const void *buff, const int order)
{
    uint8_t *ptr = (uint8_t *)buff;
    size_t len;

    int incr = order ? 1 : -1;

    if(buff && str)
    {
        len = strlen(str);

        if(len &1) return NULL;

        ptr += order ? 0 : len / 2 - 1;

        while(*str)
        {
            int d1 = CharToDigit(*str++);
            int d2 = CharToDigit(*str++);

            if(d1 == -1 || d2 == -1) return NULL;           
            *ptr = d1 * 16 + d2;
            ptr += incr;
        }
    }
    return buff;
}

int main(void) {

    int index = 0;
    char *str = "78deAc8912fF0f3B";
    uint8_t buff[strlen(str) / 2];

    StringToTable(str, buff, 0);

    printf("String: %s\nResult: ", str);
    for(index = 0; index < strlen(str) / 2; index++ )
    {
        printf("[0x%02hhx]", buff[index] );
    }
    printf("\n");

    StringToTable(str, buff, 1);

    printf("String: %s\nResult: ", str);
    for(index = 0; index < strlen(str) / 2; index++ )
    {
        printf("[0x%02hhx]", buff[index] );
    }
    printf("\n");

    return 0;
}

int CharToDigit(const char c)
{
    switch(c)
    {
        case 'a':
        case 'A':
            return 10;
        case 'b':
        case 'B':
            return 11;
        case 'c':
        case 'C':
            return 12;
        case 'd':
        case 'D':
            return 13;
        case 'e':
        case 'E':
            return 14;
        case 'f':
        case 'F':
            return 15;
        case '0':
            return 0;
        case '1':
            return 1;
        case '2':
            return 2;
        case '3':
            return 3;
        case '4':
            return 4;
        case '5':
            return 5;
        case '6':
            return 6;
        case '7':
            return 7;
        case '8':
            return 8;
        case '9':
            return 9;
        default:
            return -1;
    }
}

【讨论】:

    【解决方案3】:

    你可以像这样将一个字符转换为一个整数

    static inline int char2int(char Ch)
    {
        return(Ch>='0'&&Ch<='9')?(Ch-'0'):(Ch-'A'+10); 
        //assuming correct input with no lowercase letters
    }
    

    然后是两个字符

    static inline 
    int chars2int(unsigned char const Chars[2])
    {
        return (char2int(Chars[0])<<4)|(char2int(Chars[1]));
    }
    

    并通过转换每对几个字符:

    static inline int char2int(char Ch)
    {
        return(Ch>='0'&&Ch<='9')?(Ch-'0'):(Ch-'A'+10);
    }
    static inline 
    int chars2int(unsigned char const Chars[2])
    {
        return (char2int(Chars[0])<<4)|(char2int(Chars[1]));
    }
    #include <stdio.h>
    #include <string.h>
    #include <assert.h>
    int main()
    {
        char const inp[] = "DABC95C1";
        assert((sizeof(inp)-1)%2==0);
        unsigned i;
        unsigned char out[(sizeof(inp)-1)/2];
        for(i=0;i<sizeof(inp);i+=2){
            out[i/2]=chars2int((unsigned char*)inp+i);
        }
        for(i=0;i<sizeof(out);i++)
            printf("%2x\n", out[i]);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-06
      • 2012-04-29
      • 1970-01-01
      • 1970-01-01
      • 2019-10-14
      • 1970-01-01
      • 2012-04-15
      • 1970-01-01
      • 2019-11-21
      相关资源
      最近更新 更多