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

转自:http://blog.chinaunix.net/uid-20680669-id-3157274.html

我们在工作中,有时候会需要将字符串转化为16进制字符串给用户,因为ASCII中有些字符,
当我们使用printf("%s",p_ch);输出时会杂乱无章,如果采用16进制,会好很多。
因此编写程序,代码如下:

1.#include <stdio.h>

2.#include <string.h>

3.

4.int strToHex(char *ch, char *hex);

5.int hexToStr(char *hex, char *ch);

6.int hexCharToValue(const char ch);

7.char valueToHexCh(const int value);

8.int main(int argc, char *argv[])

9.{

10.    char ch[1024];

11.    char hex[1024];

12.    char result[1024];

13.    char *p_ch = ch;

14.    char *p_hex = hex;

15.    char *p_result = result;

16.    printf("please input the string:");

17.    scanf("%s",p_ch);

18.

19.    strToHex(p_ch,p_hex);

20.    printf("the hex is:%s\n",p_hex);

21.    hexToStr(p_hex, p_result);

22.    printf("the string is:%s\n", p_result);

23.    return 0;

24.}

25.

26.int strToHex(char *ch, char *hex)

27.{

28.  int high,low;

29.  int tmp = 0;

30.  if(ch == NULL || hex == NULL){

31.    return -1;

32.  }

33.

34.  if(strlen(ch) == 0){

35.    return -2;

36.  }

37.

38.  while(*ch){

39.    tmp = (int)*ch;

40.    high = tmp >> 4;

41.    low = tmp & 15;

42.    *hex++ = valueToHexCh(high); //先写高字节

43.    *hex++ = valueToHexCh(low); //其次写低字节

44.    ch++;

45.  }

46.  *hex = '\0';

47.  return 0;

48.}

49.

50.int hexToStr(char *hex, char *ch)

51.{

52.  int high,low;

53.  int tmp = 0;

54.  if(hex == NULL || ch == NULL){

55.    return -1;

56.  }

57.

58.  if(strlen(hex) %2 == 1){

59.    return -2;

60.  }

61.

62.  while(*hex){

63.    high = hexCharToValue(*hex);

64.    if(high < 0){

65.      *ch = '\0';

66.      return -3;

67.    }

68.    hex++; //指针移动到下一个字符上

69.    low = hexCharToValue(*hex);

70.    if(low < 0){

71.      *ch = '\0';

72.      return -3;

73.    }

74.    tmp = (high << 4) + low;

75.    *ch++ = (char)tmp;

76.    hex++;

77.  }

78.  *ch = '\0';

79.  return 0;

80.}

81.

82.int hexCharToValue(const char ch){

83.  int result = 0;

84.  //获取16进制的高字节位数据

85.  if(ch >= '0' && ch <= '9'){

86.    result = (int)(ch - '0');

87.  }

88.  else if(ch >= 'a' && ch <= 'z'){

89.    result = (int)(ch - 'a') + 10;

90.  }

91.  else if(ch >= 'A' && ch <= 'Z'){

92.    result = (int)(ch - 'A') + 10;

93.  }

94.  else{

95.    result = -1;

96.  }

97.  return result;

98.}

99.

100.char valueToHexCh(const int value)

101.{

102.  char result = '\0';

103.  if(value >= 0 && value <= 9){

104.    result = (char)(value + 48); //48为ascii编码的‘0’字符编码值

105.  }

106.  else if(value >= 10 && value <= 15){

107.    result = (char)(value - 10 + 65); //减去10则找出其在16进制的偏移量,65为ascii的'A'的字符编码值

108.  }

109.  else{

110.    ;

111.  }

112.

113.  return result;

114.}


代码经过编译,执行如下:
$ 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

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值