Chinaunix首页 | 论坛 | 博客
  • 博客访问: 262859
  • 博文数量: 50
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 919
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-03 19:18
个人简介

非淡泊无以明志,非宁静无以致远

文章分类

全部博文(50)

文章存档

2018年(2)

2017年(6)

2016年(7)

2015年(10)

2014年(11)

2013年(14)

我的朋友

分类: C/C++

2015-05-06 09:45:27


点击(此处)折叠或打开

  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.     {
  29.         return -1;
  30.     }

  31.     if(strlen(ch) == 0)
  32.     {
  33.         return -2;
  34.     }

  35.     while(*ch)
  36.     {
  37.         tmp = (int)*ch;
  38.         high = tmp >> 4;
  39.         low = tmp & 15;
  40.         *hex++ = valueToHexCh(high); //先写高字节
  41.         *hex++ = valueToHexCh(low); //其次写低字节
  42.         ch++;
  43.     }

  44.     *hex = '\0';
  45.     return 0;
  46. }

  47. int hexToStr(char *hex, char *ch)
  48. {
  49.     int high,low;
  50.     int tmp = 0;

  51.     if(hex == NULL || ch == NULL)
  52.     {
  53.         return -1;
  54.     }

  55.     if(strlen(hex) %2 == 1)
  56.     {
  57.         return -2;
  58.     }

  59.     while(*hex)
  60.     {
  61.         high = hexCharToValue(*hex);
  62.         if(high < 0)
  63.         {
  64.             *ch = '\0';
  65.             return -3;
  66.         }
  67.         
  68.         hex++; //指针移动到下一个字符上

  69.         low = hexCharToValue(*hex);

  70.         if(low < 0)
  71.         {
  72.             *ch = '\0';
  73.             return -3;
  74.         }

  75.         tmp = (high << 4) + low;
  76.         *ch++ = (char)tmp;
  77.         hex++;
  78.     }
  79.     *ch = '\0';
  80.     return 0;
  81. }



  82. int hexCharToValue(const char ch)
  83. {
  84.     int result = 0;

  85.     //获取16进制的高字节位数据
  86.     if(ch >= '0' && ch <= '9')
  87.     {
  88.         result = (int)(ch - '0');
  89.     }
  90.     else if(ch >= 'a' && ch <= 'z')
  91.     {
  92.         result = (int)(ch - 'a') + 10;
  93.     }
  94.     else if(ch >= 'A' && ch <= 'Z')
  95.     {
  96.         result = (int)(ch - 'A') + 10;
  97.     }
  98.     else
  99.     {
  100.         result = -1;
  101.     }

  102.     return result;
  103. }

  104. char valueToHexCh(const int value)
  105. {
  106.     char result = '\0';
  107.     if(value >= 0 && value <= 9)
  108.     {
  109.         result = (char)(value + 48); //48为ascii编码的‘0’字符编码值
  110.     }
  111.     else if(value >= 10 && value <= 15)
  112.     {
  113.         result = (char)(value - 10 + 65); //减去10则找出其在16进制的偏移量,65为ascii的'A'的字符编码值
  114.     }
  115.     else
  116.     {
  117.         ;
  118.     }

  119.     return result;
  120. }

阅读(3098) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~