将16进制的字符串转换为对应的字符
在C的编程中, 有时需要将16进制的ASCII码字符串转换为对应的字符串并显示出来。这里是我参考网上代码写的转换程序:
该程序分为两个部分:
一、先将对应的16进制的ASCCII字符串转换为对应的字符串
int Hex2Str(char * Hex,char * Str)
{
int high = 0,low = 0;
int temp = 0;
if(NULL==Hex || NULL==Str){
printf("the Hex or Str is empty\n");
return -1;
}
if((strlen(Hex)&1)!=0){
printf("the input Hex is wrong\n");
return -2;
}
while(*Hex){
high = HexChar2Value(*Hex);
if(high<0){
*Str = '\0';
printf("the high of Hex can not be converted to value\n");
return -3;
}
Hex++;
low = HexChar2Value(*Hex);
if(low<0){
*Str= '\0';
printf("the low of Hex can not be converted to value\n");
return -4;
}
temp = (high<<4) + low;
*Str = (char)temp;
Str++;
Hex++;
}
*Str = '\0';
return 0;
}
二、上述代码中用到了将16进制的字符转换为对应的int型的数字
int HexChar2Value(const char HexChar)
{
int result = 0;
if(HexChar>='0' && HexChar <='9'){
result = (int)(HexChar - '0');
}
else if(HexChar>='a' && HexChar <='f'){
result = (int)(HexChar - 'a')+10;
}
else if(HexChar>='A' && HexChar<='F'){
result = (int)(HexChar - 'A') + 10;
}
else {
printf("fail to convert HexChar to Value\n");
return -1;
}
return result;
}
程序的全图如下:
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
int HexChar2Value(const char HexChar)
{
int result = 0;
if(HexChar>='0' && HexChar <='9'){
result = (int)(HexChar - '0');
}
else if(HexChar>='a' && HexChar <='f'){
result = (int)(HexChar - 'a')+10;
}
else if(HexChar>='A' && HexChar<='F'){
result = (int)(HexChar - 'A') + 10;
}
else {
printf("fail to convert HexChar to Value\n");
return -1;
}
return result;
}
int Hex2Str(char * Hex,char * Str)
{
int high = 0,low = 0;
int temp = 0;
if(NULL==Hex || NULL==Str){
printf("the Hex or Str is empty\n");
return -1;
}
if((strlen(Hex)&1)!=0){
printf("the input Hex is wrong\n");
return -2;
}
while(*Hex){
high = HexChar2Value(*Hex);
if(high<0){
*Str = '\0';
printf("the high of Hex can not be converted to value\n");
return -3;
}
Hex++;
low = HexChar2Value(*Hex);
if(low<0){
*Str= '\0';
printf("the low of Hex can not be converted to value\n");
return -4;
}
temp = (high<<4) + low;
*Str = (char)temp;
Str++;
Hex++;
}
*Str = '\0';
return 0;
}
int main()
{
char *a = "4f4b";
char b[512];
Hex2Str(a,b);
printf("%s\n",b);
return 0;
}
PS:第二步的功能与atoi有点类似,但atoi只能用来转换0到9的字符,无法转换A到F的字符
上述的程序的使用范围有一定的局限性,其仅适用于字符串,无法用于其他数据类型的转换,故编写了另一个程序:
static int HexChar2Value(const char HexChar)
{
int result = 0;
//NbwfPrintf("HexChar2Value %c\n",HexChar);
if(HexChar>='0' && HexChar<='9'){
result = (int)(HexChar - '0');
// NbwfPrintf("HexChar2Value %d\n",result);
}
else{
if(HexChar>='a' && HexChar <='f'){
result = (int)(HexChar - 'a')+10;
}
else {
if(HexChar>='A' && HexChar<='F'){
result = (int)(HexChar - 'A') + 10;
}
else {
printf("fail to convert HexChar to Value\n");
return -1;
}
}
}
return result;
}
static int Hex2Str(char * Hex,char * Str,char Hex_Len)
{
int high = 0,low = 0;
int temp = 0;
int temp_len = (int)Hex_Len;
if(NULL==Hex || NULL==Str){
printf("the Hex or Str is empty\n");
return -1;
}
if((temp_len&1)!=0){
printf("the input Hex is wrong\n");
return -2;
}
temp_len = temp_len/2;
while(temp_len--){
high = HexChar2Value(*Hex);
if(high<0){
*Str = '\0';
printf("the high of Hex can not be converted to value\n");
return -3;
}
Hex++;
low = HexChar2Value(*Hex);
if(low<0){
*Str= '\0';
printf("the low of Hex can not be converted to value\n");
return -4;
}
temp = (high<<4) + low;
*Str = (char)temp;
Str++;
Hex++;
}
*Str = '\0';
return 0;
}
该程序与之前程序的最大区别在与转换的结束是以传入的长度来结束而不是字符的NULL标志来结束,从而提高了其使用范围。
将16进制的字符串转换为对应的字符相关推荐
- Java 字符串,byte[],16进制的字符串互转
Java 字符串,byte[],16进制的字符串互转 /** * 字符串转换成十六进制字符串 */ public static String str2HexStr(String str) { char ...
- ByteArray、16进制、字符串之间的转换
ByteArray.16进制.字符串之间的转换: package fengzi.convert {import flash.utils.ByteArray;public class ByteArray ...
- 16进制转字符串字符串转16进制
//16进制转字符串 void HexToStr(char *pbDest, char *pbSrc, int nLen) { unsigned char ddl,ddh; int i ...
- php把接收到的16进制转成字符串,php将16进制转为字符串的方法
php将16进制转为字符串的方法 发布时间:2020-07-18 09:24:23 来源:亿速云 阅读:81 作者:清晨 这篇文章将为大家详细讲解有关php将16进制转为字符串的方法,小编觉得挺实用的 ...
- QT 实现16进制与字符串互转
QT 实现16进制与字符串互转 文章目录 QT 实现16进制与字符串互转 前言 一.字符串QString转换16进制 二.16进制转换为字符串QString 三.正则表达式限制输入16进制 四.文本自 ...
- C# 16进制与字符串、字节数组之间的转换
在串口通讯过程中,经常要用到 16进制与字符串.字节数组之间的转换 字符串转16进制字节数组 1 private static byte[] strToToHexByte(strin ...
- python 16进制补零_Python输出16进制不带0x补零,整数转16进制,字符串转16进制
python输出16进制不带0x补零,整数转16进制,字符串转16进制 在开发中,我们偶尔会遇到需要将数据通过控制台打印出来,以检查数据传输的准确性.例如调试服务端刚接到的二进制数据(里面包含很多非可 ...
- c语言 乱码转化为16进制_C语言版的16进制与字符串互转函数
http://www.cnblogs.com/nio-nio/p/3309367.html /* // C prototype : void StrToHex(BYTE *pbDest, BYTE * ...
- Python输出16进制不带0x补零,整数转16进制,字符串转16进制
Python输出16进制不带0x补零,整数转16进制,字符串转16进制 在开发中,我们偶尔会遇到需要将数据通过控制台打印出来,以检查数据传输的准确性.例如调试服务端刚接到的二进制数据(里面包含很多 ...
- 字符串(含中文)转16进制,16进制转字符串(含中文)
字符串(含中文)转16进制 public static byte[] SendS(String str){byte[] ok = new byte[0]; try {ok = str.getBytes ...
最新文章
- YUM环境的三种搭建方法
- (015)java后台开发之web项目中如何添加jar包和删除jar包
- 区块链技术之Fabric逻辑架构详解
- CSDN的常用文本设置(字体大小红色)
- Vcenter6.5 Administrator账户密码忘记或者过期无法登陆解决办法
- python float精度问题_Python之☞float浮点数精度问题
- oracle last_day比较,Oracle的LAST_DAY函数
- 大数据建模,eBay的一个牛人
- LINUX yum 服务
- poj1511 最短路
- android自动点击相应位置脚本,轻易连使用说明-自动连点器-安卓自动点击脚本 | MOS86...
- ESXi 安装 OpenWRT
- .net core | donet core IIS 文件路径问题
- Python深度学习之LSTM文本生成
- 自动控制系统实验总结
- Nginx源码阅读:ngx_palloc 内存池
- RM-MEDA的理解与分析
- 分析一个简单的汇编代码
- 史兴国对谈顾振清:NFT艺术有哪些可以抵御加密寒冬的“武器”?
- 【嵌入式算法】空间向量夹角公式及其应用