简体   繁体   English

在1字节的C 2个字符中将十六进制char []转换为int []

[英]Convert hex char[] to int[] in C 2 chars in 1 byte

I am trying to convert a char[] in hexadecimal format to int[] in hexadecimal. 我试图将十六进制格式的char []转换为十六进制的int []。

Something like this: 像这样的东西:

hello --> 68656C6C6F --> [68, 65, 6C, 6C, 6F] 你好 - > 68656C6C6F - > [68,65,6C,6C,6F]

This is my code: 这是我的代码:

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

uint8_t* hex_decode(unsigned char *in, size_t len, uint8_t *out);

int main(void){
unsigned char  word_in[17], word_out[33];//17:16+1, 33:16*2+1
int i, len = 0;
uint8_t* out;


while(len != 16){
    printf("Set new word:");
    fgets( word_in, sizeof( word_in), stdin);
    len = strlen( word_in);
    if( word_in[len-1]=='\n')
        word_in[--len] = '\0';

    for(i = 0; i<len; i++){
        sprintf(word_out+i*2, "%02X",  word_in[i]);
    }
    if(len != 16){
        printf("Please, use a word of 16 chars long\n\n");
    }
}
printf("%s", word_in);
printf("\n");

hex_decode(word_out, sizeof(word_out), out);

return 0;
}

uint8_t* hex_decode(unsigned char *in, size_t len, uint8_t *out)
{
    unsigned int i, t, hn, ln;

    for (t = 0,i = 0; i < len; i+=2,++t) {

            hn = in[i] > '9' ? (in[i]|32) - 'a' + 10 : in[i] - '0';
            ln = in[i+1] > '9' ? (in[i+1]|32) - 'a' + 10 : in[i+1] - '0';

            out[t] = (hn << 4 ) | ln;
            printf("%s",out[t]);
    }
    return out;

}

But after printing the word, I got a segmentation fault. 但在打印完这个单词后,我遇到了分段错误。

That function works perfect in arduino so I think it should works fine at my computer... Where is the problem? 这个功能在arduino中运行得很完美,所以我觉得它在我的电脑上运行正常......问题出在哪里?

You get a segmentation fault because you are passing the pointer out before making any assignments to it. 您会收到分段错误,因为您在对其进行任何分配之前将指针out Either the hex_decode need to take uint8_t **out_ptr and assign it a dynamically allocated array, or the caller needs to provide an array sufficient to hold the output of the conversion. hex_decode需要取uint8_t **out_ptr并为其分配动态分配的数组,或者调用者需要提供足以保存转换输出的数组。

The reason why it "works" on another platform is that it exhibits undefined behavior : in arduino, the arbitrary value placed in the uninitialized pointer out happens to point to an unused location in memory. 为什么它在其他平台上“工程”的原因是,它表现出不确定的行为 :在Arduino的,放置在未初始化的指针的任意值out正好指向内存中未使用的位置。 Writing to that location does not trigger segmentation fault, creating an illusion of working code. 写入该位置不会触发分段错误,从而产生工作代码的错觉。

See @dasblinkenlight answer for seg fault. 有关seg故障,请参阅@dasblinkenlight答案。 To decode 2 bytes: 要解码2个字节:

My 50 C ent... (a short version) 我的50 C ...(简短版)

char hex[3];
char * phex;
int result;
for(int i = 0; i < 256; i++)
{
    sprintf(hex, "%02X", i);
    phex = hex;
    result = ((*phex > 64 ? (*phex & 0x7) + 9 : *phex - 48) << 4) | (*(phex+1) > 64 ? (*(phex+1) & 0x7) + 9 : *(phex+1) - 48);
    if(result != i)
    {
        printf("err %s %02X\n", hex, result);
    }
}

Code above does no validation. 上面的代码没有验证。 This procedure returns -1 when input was invalid. 输入无效时,此过程返回-1。

int h2i(char * ph)
{
    int result;
    if(*ph > 96 && *ph < 103) result = *ph - 87;
    else if(*ph > 64 && *ph < 71) result = *ph - 55;
    else if(*ph > 47 && *ph < 59) result = *ph - 48;
    else return -1;
    result <<= 4;
    ph++;
    if(*ph > 96 && *ph < 103) result |= *ph - 87;
    else if(*ph > 64 && *ph < 71) result |= *ph - 55;
    else if(*ph > 47 && *ph < 59) result |= *ph - 48;
    else return -1;
    return result;
}

But wait? 可是等等? A char can also be -1. char也可以是-1。 Yes, after casting. 是的,铸造后。

char * x = "FF";
char y;
int result;
result = h2i(x);
// if (result == -1) ...error...
y = (char)result;

The program looks complicated comparing what you want to do. 与您想要做的相比,该程序看起来很复杂。

if you want to print the hexadecimal ascii code of a charachter, you can simply use 如果你想打印charachter的十六进制ascii代码,你可以简单地使用

printf("%02X",'K'); // this will display the code ascii of 'K' in hexadecimal

If you want to print your word in code ascii in another char array. 如果要在另一个char数组中的代码ascii中打印单词。 you can use sprintf() : 你可以使用sprintf()

int main() {
        char word_in[17]="hello", word_out[33];
        char *pi = word_in, *po = word_out;
        word_out[0]=0;

        for (;*pi;po+=2,pi++)
           sprintf(po,"%02X",*pi);

        printf("%s\n", word_out);
}

A charachetr is saved in binary format in the memory. charachetr以二进制格式保存在内存中。 this binary format represent the code ascii of the charachter. 这个二进制格式代表charachter的代码ascii。 And when you want to print its content: 当您想要打印其内容时:

  • when using "%d" : this will print the code ascii as integer 使用"%d" :这将打印代码ascii为整数
  • when using "%x" : this will print the code ascii as hexadecimal 使用"%x" :这会将代码ascii打印为十六进制
  • when using "%c" : this will print the charachter 当使用"%c" :这将打印charachter

i will just share my own code for this: 我将分享我自己的代码:

it converts any 8 hexadecimal char string into a integer number from [-2147483648. 它将任何8个十六进制char字符串转换为[-2147483648中的整数。 2147483647] input(argument) is 1 string(8+'\\0'), output(returns) is a long int, MODIFY AS NECESSARY 2147483647] input(参数)是1个字符串(8 +'\\ 0'),输出(返回)是一个long int,MODIFY AS NECESSARY

#define N 8

long int hex2dec(char* hex){        /*conversor HEX 2 DEC*/
    int i,j,n[N],l,neg;
    long int dec=0;

    for(i=0;i<N;i++){
        n[i]=0;
    }
    l=strlen(hex);

    neg=0;
    if(hex[0]>='8'){
        neg=1;
        for(i=0;i<N;i++){
            if(hex[i]=='0'){
                hex[i]='F';
                continue;
            }
            if(hex[i]=='1'){
                hex[i]='E';
                continue;
            }
            if(hex[i]=='2'){
                hex[i]='D';
                continue;
            }
            if(hex[i]=='3'){
                hex[i]='C';
                continue;
            }
            if(hex[i]=='4'){
                hex[i]='B';
                continue;
            }
            if(hex[i]=='5'){
                hex[i]='A';
                continue;
            }
            if(hex[i]=='6'){
                hex[i]='9';
                continue;
            }
            if(hex[i]=='7'){
                hex[i]='8';
                continue;
            }
            if(hex[i]=='8'){
                hex[i]='7';
                continue;
            }
            if(hex[i]=='9'){
                hex[i]='6';
                continue;
            }
            if(hex[i]=='A'){
                hex[i]='5';
                continue;
            }
            if(hex[i]=='B'){
                hex[i]='4';
                continue;
            }
            if(hex[i]=='C'){
                hex[i]='3';
                continue;
            }
            if(hex[i]=='D'){
                hex[i]='2';
                continue;
            }
            if(hex[i]=='E'){
                hex[i]='1';
                continue;
            }
            if(hex[i]=='F'){
                hex[i]='0';
                continue;
            }
        }
    }

    for(i=0;i<N;i++){
        switch(hex[i]){
        case '0':
            n[i]=hex[i]-48;  /* Ascii '0'=48 48-48=0*/
            break;
        case '1':
            n[i]=hex[i]-48;  /* Ascii '1'=49 49-48=1*/
            break;
        case '2':
            n[i]=hex[i]-48;
            break;
        case '3':
            n[i]=hex[i]-48;
            break;
        case '4':
            n[i]=hex[i]-48;
            break;
        case '5':
            n[i]=hex[i]-48;
            break;
        case '6':
            n[i]=hex[i]-48;
            break;
        case '7':
            n[i]=hex[i]-48;
            break;
        case '8':
            n[i]=hex[i]-48;
            break;
        case '9':
            n[i]=hex[i]-48;
            break;
        case 'A':
            n[i]=hex[i]-55;  /* Ascii 'A'=65 65-55=10*/
            break;
        case 'B':
            n[i]=hex[i]-55;  /* Ascii 'B'=66 66-55=11*/
            break;
        case 'C':
            n[i]=hex[i]-55;
            break;
        case 'D':
            n[i]=hex[i]-55;
            break;
        case 'E':
            n[i]=hex[i]-55;
            break;
        case 'F':
            n[i]=hex[i]-55;
            break;
        }
    }
    for(i=0,j=l;i<l;i++,j--){
        dec=dec+(n[j-1]*pow(16,i));
    }
    if(neg==1){
        dec=0-dec;
        dec=dec-1;
    }
    return dec;

}

change 更改

uint8_t *out;//region is not ensured

to

uint8_t out[sizeof(word_out)/2];

change 更改

hex_decode(word_out, sizeof(word_out), out);//sizeof(word_out) is 33, must to 32

to

hex_decode(word_out, strlen(word_out), out);//strlen(word_out) or len * 2 or sizeof(word_out) -1

change 更改

printf("%s",out[t]);//out is not string

to

printf("%02X ",out[t]);

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

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