2013-05-13 23 views
0

我試圖將十六進制格式的char []轉換爲十六進制的int []。在1個字節的C 2個字符中將十六進制字符[]轉換爲int []

事情是這樣的:

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

這是我的代碼:

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

} 

但印刷後這個詞,我得到了一個分割錯誤。

該函數在arduino中完美工作,所以我認爲它應該可以在我的電腦上正常工作......問題在哪裏?

回答

1

見@dasblinkenlight回答賽格故障。爲了解碼2個字節:

我50 ç耳鼻喉科...(短版)上述

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

代碼做任何驗證。輸入無效時此過程返回-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; 
} 

但是等一下? char也可以是-1。是的,鑄造後。

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

由於您在對其進行任何分配之前傳遞指針out,所以會出現分段錯誤。 hex_decode需要採用uint8_t **out_ptr併爲其分配一個動態分配的數組,或者調用者需要提供足以容納轉換輸出的數組。

它之所以在其他平臺上「作品」,是它表現出未定義行爲:在Arduino的,放置在未初始化的指針out任意值正好指向內存中未使用的位置。寫入該位置不會觸發分段錯誤,從而產生工作代碼的錯覺。

0

該程序看起來比較複雜,你想要做什麼。

,如果你想打印charachter的十六進制ASCII代碼,你可以簡單地如果你想打印在另一個字符數組您在ascii碼字使用

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

。您可以使用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); 
} 

一個charachetr在記憶二進制格式保存。這個二進制格式代表了charachter的代碼ascii。而當你要打印的內容:

  • 使用"%d"時:使用"%c"時,這會顯示ascii碼爲十六進制
  • :這將使用"%x"當打印ascii碼爲整數
  • 這將打印charachter
+0

我不需要打印它,我需要將它保存到一個數值(int,uint8_t ...)數組作爲參數傳遞給其他函數。但是,謝謝,mybe我可以使用它的一部分。 – Biribu 2013-05-14 07:32:14

+0

@Biribu char已經以數字格式保存。數字格式表示其代碼ascii。所以你可以直接使用原始字符數組作爲數字,並將其作爲參數傳遞給其他函數。無需轉換。你可以讀取原始數組作爲十六進制 – MOHAMED 2013-05-14 08:16:47

0

我只想分享我自己的代碼如下:

其轉換任意8個十六進制char字符串轉換爲[-2147483648。2147483647] 輸入(參數)是1串(8 + '\ 0'),輸出(返回)是一個長整型,根據需要修改

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

} 
+0

謝謝,但我已經將字符串轉換爲十六進制。我只需要如何加入2個字符來製作十六進制數:「1 \ 0」 - >「31 \ 0」 - > 31(int)< - 這是我需要的。 – Biribu 2013-05-14 07:40:23

+0

有沒有聽說過switch語句? – JeremyP 2015-05-29 10:10:33

0

變化

uint8_t *out;//region is not ensured 

uint8_t out[sizeof(word_out)/2]; 

變化

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

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

變化

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

printf("%02X ",out[t]); 
相關問題