牛骨文教育服务平台(让学习变的简单)
博文笔记

字符串和16进制字符串的相互转化

创建时间:2017-03-22 投稿人: 浏览次数:449
我们在工作中,有时候会需要将字符串转化为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 ",p_hex);
  19.     hexToStr(p_hex, p_result);
  20.     printf("the string is:%s ", 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 = "";
  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) %2 == 1){
  52.     return -2;
  53.   }

  54.   while(*hex){
  55.     high = hexCharToValue(*hex);
  56.     if(high < 0){
  57.       *ch = "";
  58.       return -3;
  59.     }
  60.     hex++; //指针移动到下一个字符上
  61.     low = hexCharToValue(*hex);
  62.     if(low < 0){
  63.       *ch = "";
  64.       return -3;
  65.     }
  66.     tmp = (high << 4) + low;
  67.     *ch++ = (char)tmp;
  68.     hex++;
  69.   }
  70.   *ch = "";
  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 = "";
  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:!@#$%^&*()_+~`1234567890-=
the hex is:21402324255E262A28295F2B7E60313233343536373839302D3D
the string is:!@#$%^&*()_+~`1234567890-=

经测试,本代码如果输入中含有空格,则空格以后的字符串无法进行转化
测试的环境:
编译器: gcc 4.4.3
操作系统:ubuntu 10.04.4
内核版本:2.6.32-40-generic
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。