1. 程式人生 > >字串和16進位制字串的相互轉化

字串和16進位制字串的相互轉化

我們在工作中,有時候會需要將字串轉化為16進位制字串給使用者,因為ASCII中有些字元,
當我們使用printf("%s",p_ch);輸出時會雜亂無章,如果採用16進位制,會好很多。
因此編寫程式,程式碼如下:

點選(此處)摺疊或開啟

  1. #include <stdio.h>
  2. #include <string.h>
  3. int strToHex(char *ch, char *hex);
  4. int hexToStr(char *hex, char *ch);
  5. int hexCharToValue(const char ch);
  6. char valueToHexCh(const
     int value);
  7. int main(int argc, char *argv[])
  8. {
  9.     char ch[1024];
  10.     char hex[1024];
  11.     char result[1024];
  12.     char *p_ch = ch;
  13.     char *p_hex = hex;
  14.     char *p_result = result;
  15.     printf("please input the string:");
  16.     scanf("%s",p_ch);
  17.     strToHex(p_ch,p_hex);
  18.     printf("the hex is:%s\n",p_hex)
    ;
  19.     hexToStr(p_hex, p_result);
  20.     printf("the string is:%s\n", p_result);
  21.     return 0;
  22. }
  23. int strToHex(char *ch, char *hex)
  24. {
  25.   int high,low;
  26.   int tmp = 0;
  27.   if(ch == NULL || hex == NULL){
  28.     return -1;
  29.   }
  30.   if(strlen(ch) == 0){
  31.     return -2;
  32.   }
  33.   while(*ch){
  34.     tmp = (int)*ch;
  35.     high = tmp >
    > 4;
  36.     low = tmp & 15;
  37.     *hex++ = valueToHexCh(high); //先寫高位元組
  38.     *hex++ = valueToHexCh(low); //其次寫低位元組
  39.     ch++;
  40.   }
  41.   *hex = '\0';
  42.   return 0;
  43. }
  44. int hexToStr(char *hex, char *ch)
  45. {
  46.   int high,low;
  47.   int tmp = 0;
  48.   if(hex == NULL || ch == NULL){
  49.     return -1;
  50.   }
  51.   if(strlen(hex) %== 1){
  52.     return -2;
  53.   }
  54.   while(*hex){
  55.     high = hexCharToValue(*hex);
  56.     if(high < 0){
  57.       *ch = '\0';
  58.       return -3;
  59.     }
  60.     hex++; //指標移動到下一個字元上
  61.     low = hexCharToValue(*hex);
  62.     if(low < 0){
  63.       *ch = '\0';
  64.       return -3;
  65.     }
  66.     tmp = (high << 4) + low;
  67.     *ch++ = (char)tmp;
  68.     hex++;
  69.   }
  70.   *ch = '\0';
  71.   return 0;
  72. }
  73. int hexCharToValue(const char ch){
  74.   int result = 0;
  75.   //獲取16進位制的高位元組位資料
  76.   if(ch >= '0' && ch <= '9'){
  77.     result = (int)(ch - '0');
  78.   }
  79.   else if(ch >= 'a' && ch <= 'z'){
  80.     result = (int)(ch - 'a') + 10;
  81.   }
  82.   else if(ch >= 'A' && ch <= 'Z'){
  83.     result = (int)(ch - 'A') + 10;
  84.   }
  85.   else{
  86.     result = -1;
  87.   }
  88.   return result;
  89. }
  90. char valueToHexCh(const int value)
  91. {
  92.   char result = '\0';
  93.   if(value >= 0 && value <= 9){
  94.     result = (char)(value + 48); //48為ascii編碼的‘0’字元編碼值
  95.   }
  96.   else if(value >= 10 && value <= 15){
  97.     result = (char)(value - 10 + 65); //減去10則找出其在16進位制的偏移量,65為ascii的'A'的字元編碼值
  98.   }
  99.   else{
  100.     ;
  101.   }
  102.   return result;
  103. }
程式碼經過編譯,執行如下:
$ gcc conver.c
c$ ./a.out 
please input the string:[email protected]#$%^&*()_+~`1234567890-=
the hex is:21402324255E262A28295F2B7E60313233343536373839302D3D
the string is:[email protected]#$%^&*()_+~`1234567890-=

經測試,本程式碼如果輸入中含有空格,則空格以後的字串無法進行轉化
測試的環境:
編譯器: gcc 4.4.3
作業系統:ubuntu 10.04.4

核心版本:2.6.32-40-generic