将字符串转换为与之对应的16进制字符串
来源:互联网 发布:麒麟啤酒 中国 知乎 编辑:程序博客网 时间:2024/06/10 19:49
在C的编程中, 有时需要将字符串转换为对应的16进制的ASCII码字符串并显示出来。这里是我参考网上代码写的转换程序:
该程序分为两个部分:
一、先将字符串转换为对应的16进制的数值
int Str2Hex(char *str,char *hex){int high = 0;int low = 0;int temp = 0;if (NULL==str || NULL==hex){printf("the str or hex is wrong\n");return -1;}if (0==strlen(str)){printf("the input str is wrong\n");return -2;}while(*str){temp = (int)(*str);high = temp>>4;low = temp & 15;*hex = Value2Hex(high);hex++;*hex = Value2Hex(low);hex++;str++;}*hex = '\0';return 0;}
char Value2Hex(const int value){char Hex = NULL;if (value>=0 && value<=9){Hex = (char)(value+'0');}else{if (value>9 && value<16){Hex = (char)(value-10+'A');}else{printf("the input value is out of range\n");}}return Hex;}
# include <stdio.h>
# include <string.h>
char Value2Hex(const int value)
{
char Hex = NULL;
if (value>=0 && value<=9){
Hex = (char)(value+'0');
}
else{
if (value>9 && value<16){
Hex = (char)(value-10+'A');
}
else{
printf("the input value is out of range\n");
}
}
return Hex;
}
int Str2Hex(char *str,char *hex)
{
int high = 0;
int low = 0;
int temp = 0;
if (NULL==str || NULL==hex){
printf("the str or hex is wrong\n");
return -1;
}
if (0==strlen(str)){
printf("the input str is wrong\n");
return -2;
}
while(*str){
temp = (int)(*str);
high = temp>>4;
low = temp & 15;
*hex = Value2Hex(high);
hex++;
*hex = Value2Hex(low);
hex++;
str++;
}
*hex = '\0';
return 0;
}
int main()
{
char * a = "OK";
char b[512];
Str2Hex(a,b);
printf("%s\n",b);
return 0;
}
当然,还有一种简单粗暴的方法,便是通过sprintf来进行转换,但其有一个限制便是只能一个一个字符地进行转换。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[512];
char b= 'K';
sprintf(a,"%x",b);
printf("%s\n",a);
return 0;
}
PS:在第二步中,将整型数值转换为字符型数值也可通过itoa实现。
上述的转换程序会存在着一些的缺陷,其无法用于将非字符串的数据进行转换,故另一个程序编写来进行转换,其如下:
static char Value2Hex(const int value){char hex = NULL;if(value>=0 && value<=9){hex = (char)(value+'0');}else{if(value>9 && value<16){hex = (char)(value-10+'A');}else {NbwfPrintf("the input value in the function Value2Hex is out of range\n");usleep(1000*1000);}}return hex;}static int Str2Hex(char *str, char *hex, char Str_Len){int high = 0;int low = 0;int result = 0;int temp_len = (int)Str_Len;int temp =0;if(NULL==str || NULL==hex){NbwfPrintf("the str or hex is wrong\n");usleep(1000*1000);result = 1;}if(0==temp_len){NbwfPrintf("the Str2Hex need the length of the str\n");usleep(1000*1000);return -2;}while(temp_len--){temp = (int)(*str);high = temp>>4;low = temp & 15;*hex = Value2Hex(high);hex++;*hex = Value2Hex(low);hex++;str++;}*hex = '\0';return 0;}
- 将字符串转换为与之对应的16进制字符串
- 将由16进制数字组成的字符串转换成与之等价的整形值
- 将16进制的字符串转换为对应的字符
- 将一个字符串转换为对应的整数
- 将整数n这个数字转换为对应的字符串
- 将16进制字符串转换为整数的函数
- 将16进制的字符串 序列,转换 为汉子字符串
- 将字符串转换为16进制的字符串(常用在串口编程中)
- ascii码转换为对应的字符串
- 把字符串转换为对应的整数
- 将8进制字符串转换为整数的代码
- strtol----将16进制字符串转换为整数
- 将字符串转换为16进制保存
- 如何将16进制字符串转换为Int32类型
- qt将16进制数据转换为字符串
- 将16进制字符串转换为整数输出
- C#将指定字符串转换为枚举类型对应的值
- 实现一个函数int my_atoi(char s[]),可以将一个字符串转换为对应的整数。
- HDU-6113 度度熊的01世界
- 【floor函数】【ceil函数】
- Hdu6113度度熊的01世界(2017"百度之星"程序设计大赛
- hdu 6109 数据分割 并查集缩点建边
- E
- 将字符串转换为与之对应的16进制字符串
- 8.12 总结
- HPU1411-- QAQ & ORZ 的签到题
- 整数奇偶排序
- hdu 6108小C的倍数问题(2017百度之星初赛一1001)
- 初探Java多线程
- TOP 10 开源的推荐系统简介
- 日记8/12
- HDU 4283 You Are the One (区间DP)