16进制串与ASCII字符串相互转换
提供两个函数,方便十六进制串与ASCII 字符串之间的相互转换,使用函数需要注意的是返回的串是在堆上通过 calloc 分配的,所以,记得使用完返回值释放该块,并且将指向该块的指针 =NULL 。
// 函数输入字符串,输出字符串对应的16进制串
char *chstohex ( char* chs )
{
char hex[16] = { '0', '1', '2', '3', '4', '5', '6', \
'7', '8','9', 'A', 'B', 'C', 'D', 'E', 'F'
};
int len = strlen ( chs );
char* ascii = NULL ;
ascii = (char*)calloc ( len * 3 + 1, sizeof(char) ); // calloc ascii
int i = 0;
while( i < len )
{
ascii[i*2] = hex[(int)( (char)chs[i] / 16 )] ;
ascii[i*2 + 1] = hex[(int)( (char)chs[i] % 16 )] ;
++i;
}
return ascii; // ascii 返回之前未释放
}
// 函数输入16进制字符串,输出对应的字符串
char *hextochs ( char* ascii )
{
int len = strlen ( ascii ) ;
if( len%2 != 0 )
return NULL ;
char *chs = NULL ;
chs = (char*)calloc( len / 2 + 1, sizeof(char) ); // calloc chs
int i = 0 ;
char ch[2] = {0};
while( i < len )
{
ch[0] = ( (int)ascii[i] > 64 ) ? ( ascii[i]%16 + 9 ) : ascii[i]%16 ;
ch[1] = ( (int)ascii[i + 1] > 64 ) ? ( ascii[i + 1]%16 + 9 ) : ascii[i + 1]%16 ;
chs[i/2] = (char)( ch[0]*16 + ch[1] );
i += 2;
}
return chs ; // chs 返回前未释放
}
以上转自:http://blog.163.com/163_dhbing/blog/static/11738780320091086511358/
/*
字符串转16进制
*/
void StrToHex(BYTE *pbDest, BYTE *pbSrc, int nLen)
{
char ddl,ddh;
int i;
for (i=0; i<nLen; i++)
{
ddh = 48 + pbSrc[i] / 16;
ddl = 48 + pbSrc[i] % 16;
if (ddh > 57) ddh = ddh + 7;
if (ddl > 57) ddl = ddl + 7;
pbDest[i*2] = ddh;
pbDest[i*2+1] = ddl;
}
pbDest[nLen*2] = '\0';
}
16进制串与ASCII字符串相互转换的更多相关文章
- 汇编:1位16进制数到ASCII码转换
;============================ ;1位16进制数到ASCII码转换 ; { X+30H (0≤X≤9) ;Y= { ; { X+37H (0AH≤X≤0FH) DATAS ...
- iOS开发中16进制颜色(html颜色值)字符串转为UIColor
//16进制颜色(html颜色值)字符串转为UIColor +(UIColor *) hexStringToColor: (NSString *) stringToConvert { NSString ...
- 16进制串hex与ASCII字符串相互转换
提供两个函数,方便十六进制串与ASCII 字符串之间的相互转换,使用函数需要注意的是返回的串是在堆上通过 calloc 分配的,所以,记得使用完返回值释放该块,并且将指向该块的指针 =NULL . c ...
- 适合MCU用的C语言快速互转HEX(16进制)和原始字符串/数组方法
缘由 这个起因是昨晚群里有人在讨论怎么把字符串转成HEX方法最佳,讨论到最后变成哪种方法效率最优了.毕竟这代码是要在MCU上面跑的,要同时考虑到时间和空间的最优解. 当然讨论的是有结果的,具体实现的方 ...
- Java 将数字转为16进制,然后转为字符串类型
public class ArrayTest3 { public static void main(String[] args){ System.out.println(toHex(60)); } / ...
- Java 将数字转为16进制,然后转为字符串类型 将空格去掉。终结版
//十进制转为十六进制 public class ArrayTest7 { public static void main(String[] args){ System.out.println(toH ...
- C# 发送16进制串口数据
一个困扰两天的问题:需要通过串口向设备发送的数据:0A010 7e 08 00 11 00 00 7e 76 7f我先将每个16进制字符转换成10进制,再将其转换成ASCII码对应的字符. /// & ...
- python经常使用的十进制、16进制、字符串、字节串之间的转换(长期更新帖)
进行协议解析时.总是会遇到各种各样的数据转换的问题,从二进制到十进制,从字节串到整数等等 废话不多上.直接上样例 整数之间的进制转换: 10进制转16进制: hex(16) ==> 0x10 ...
- python常用的十进制、16进制、字符串、字节串之间的转换
进行协议解析时,总是会遇到各种各样的数据转换的问题,从二进制到十进制,从字节串到整数等等 废话不多上,直接上例子 整数之间的进制转换: 10进制转16进制: hex(16) ==> 0x10 ...
随机推荐
- Codeforces 472D
看官方题解提供的是最小生成树,怎么也想不明确.you can guess and prove it! 看了好几个人的代码.感觉实现思路全都不一样,不得不佩服cf题目想法的多样性 以下说说我自己的理解, ...
- apicloud中的sqlite操作模块db
db 模块封装了手机常用数据库 sqlite 的增删改查语句,可实现数据的本地存储,极大的简化了数据持久化问题. 1.执行 var db = api.require('db'); db.execute ...
- js插件---Amaze UI dialog如何使用
js插件---Amaze UI dialog如何使用 一.总结 一句话总结:别人给你列出来的参考手册照着用先 1.在哪里去找插件参考资料或者使用手册(一般位置找不到的时候)? github上面啊,非常 ...
- 52. nodejs报错:Cannot find module 'ejs'
转自:https://blog.csdn.net/u010142437/article/details/79012605 错误显示: Error: Cannot find module 'ejs' ...
- Kinect 开发 —— 语音识别(上)
Kinect的麦克风阵列在Kinect设备的下方.这一阵列由4个独立的水平分布在Kinect下方的麦克风组成.虽然每一个麦克风都捕获相同的音频信号,但是组成阵列可以探测到声音的来源方向.使得能够用来识 ...
- windows安装memcached
http://www.cnblogs.com/wujuntian/p/4791220.html
- 03005_SQL查询语句
查询语句,在开发中使用的次数最多,此处使用“zhangwu” 账务表. 1.准备工作 (1)创建财务表: CREATE TABLE zhangwu ( id INT PRIMARY KEY AUTO_ ...
- UVA 11346 Probability (几何概型, 积分)
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=2321">https://uva ...
- 2.3 Streams API 官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ 2.3 Streams API 2.3 Streams API 在0..0增加了一个 ...
- golang beego cache
package main import ( "fmt" "github.com/astaxie/beego/cache" "time" ) ...