简体   繁体   English

C char数组到uint8_t数组

[英]c char array to uint8_t array

I have a basic question regarding ac problem I'm having. 我有一个关于交流问题的基本问题。 My input char array would be something like: 我的输入字符数组如下所示:

'DABC95C1' 

and I want to make an uint8_t array out of it 我想用它制作一个uint8_t数组

0xDA 0xBC 0x95 0xC1

I have easily access to each char but I dont know how I can form 0xDA . 我可以轻松访问每个字符,但是我不知道如何形成0xDA Is there function in c or can i just cast it? C中有函数还是我可以强制转换它?

Use the strtoull function to convert a string to a number in a given base. 使用strtoull函数可将字符串转换为给定基数的数字。 Then just shift out the desired bytes. 然后只需移出所需的字节。 Such as: 如:

#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;
}

result: 结果:

c1, 95, bc, da

Demo 演示版

Notes: 笔记:

As your requirement is to get an array of bytes, you might be tempted to do something like 由于您的要求是获取字节数组,因此您可能会想做类似的事情

uint8_t *arr = (uint8_t*)&res;

But here are two caveats in this: 但是这里有两个警告:

1) I is a strict aliasing rule violation (you can somehow to work around it by replacing uint8_t with char ) 1)我违反了严格的别名规则 (您可以通过用char替换uint8_t来解决该问题)
2) The order of the returned bytes will be implementation specific (endianness dependent) and thus not portable. 2)返回字节的顺序将是实现特定的(取决于字节序),因此不可移植。 Also note that the result is unsigned long long , so you might get extra padding zeros as either the beginning of the array or in the end of it. 还要注意,结果是unsigned long long ,因此您可能会在数组的开头或结尾处得到额外的填充零。

Any size string in the chosen order. 按所选顺序的任意大小的字符串。 Portable digit conversion and it optimizes very well on the ASCII systems. 便携式数字转换,它在ASCII系统上的优化非常好。 https://godbolt.org/g/Ycah1e 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;
    }
}

You can conver a character to an int like so 您可以像这样将字符转换为整数

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

Two characters then with 然后两个字符

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

And several characters by converting each pair: 通过转换成对的几个字符:

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]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM