Binary转换成Hex字符串
想调优别人的代码,网上搜索一下Binary to Hexstring的转换,全是利用printf、scanf之类实现的,效率好低,还是自己想个简单的办法吧!
.......此处省略一万字.......
改进后的方法:
int tohex(void* str, int strlen, char *ascii, int size)
{
if(strlen == || str== NULL || ascii==NULL)
{
if(NULL != ascii)
ascii[]=0x00;
return ;
} char* p1 = ascii;//new char[len*2+1];
unsigned char* p2 = (unsigned char*)str;
int i = , j = ;
char dict[]={'','','','','','','','','','','A','B','C','D','E','F'};
bool blCuted = false;
for( i = ; i < strlen; ++i)
{
p1[j] = dict[ (p2[i] >> )];
p1[j+] = dict[p2[i] & 0x0F];
j+=; if(j > size){
blCuted = true;
break;
}
}
if(blCuted)
j-= ;
p1[j] =0x00;
return j;
}
改进前的方法(抄的):
int BCD2ASC(const char *str, int strlen,char *ascii, int size)
{
int i = , p = , l = ;
byte ch; if(strlen == || str== NULL || ascii==NULL)
return NULL; bool blCuted = false;
while(i<strlen)
{
ch = str[i++];
l += ;
if(l > size){
blCuted = true;
break;
}
p += sprintf(ascii+p, "%02X", ch);
} if(blCuted)
l-= ;
return l;
}
测试代码:
int main( )
{
int a=0x1234;
int b=0xabcd;
char *bistr="\x12\x34\x56\x78\x90\xab\xcd\xef\xe1\xf9\x1f\x1e\x00";
char szTmp[*] = {};
tohex(&a, sizeof(int), szTmp, sizeof(szTmp)); cout << szTmp << endl;
tohex(&b, sizeof(int), szTmp, sizeof(szTmp)); cout << szTmp << endl;
tohex(bistr, strlen(bistr), szTmp, sizeof(szTmp)); cout << szTmp << endl; FILE* fp = fopen("D:\\testbinary.bi", "rb");
char szBinary[*] = {};
int ired = fread(szBinary, , *-, fp);
cout << "readlen:" << ired <<endl; DWORD dwB = GetTickCount();
for(int i = ; i < ; ++i)
{
tohex(szBinary, ired, szTmp, sizeof(szTmp)-); //i=1w,<200ms
cout << szTmp << endl;
BCD2ASC(szBinary, ired, szTmp, sizeof(szTmp)-); //i=1w,9000ms
cout << szTmp << endl;
}
DWORD dwE = GetTickCount(); cout << "cost:" << dwE-dwB << "ms" <<endl; fclose(fp);
return ;
}
效率差的不是一条街,你可以try一下。
Binary转换成Hex字符串的更多相关文章
- C#中对象,字符串,dataTable、DataReader、DataSet,对象集合转换成Json字符串方法。
C#中对象,字符串,dataTable.DataReader.DataSet,对象集合转换成Json字符串方法. public class ConvertJson { #region 私有方法 /// ...
- DataTable转换成json字符串
将DataTable里面的行转换成json字符串方法: #region DataTable转为json /// <summary> /// DataTable转为json /// < ...
- Newtonsoft.Json 把对象转换成json字符串
var resultJson = new { records = rowCount, page = pageindex, //总页数=(总页数+页大小-1)/页大小 total = (rowCount ...
- Java将其他数据格式转换成json字符串格式
package com.wangbo.util; import java.beans.IntrospectionException; import java.beans.Introspector; i ...
- json 字符串转换成对象,对象转换成json字符串
json 字符串转换成对象,对象转换成json字符串 前端: 方法一: parseJSON方法: [注意jquery版本问题] var str = '{"name":&qu ...
- PDF转换成二进制字符串写入 HTTP 输出流
最近项目需要做电子签章,需要网页打开PDF签章后保存:正好复习哈二进制和流的转换: 文件转换成二进制字符串写入HTTP输出流 protected void Page_Load(object sende ...
- JSON对象转换成JSON字符串
1.问题背景 有一个json对象,需要将其转换成json字符串 JSON.stringify(obj) 2.实现源码 <!DOCTYPE html PUBLIC "-//W3C//DT ...
- java 图片转换成base64字符串
import java.io.ByteArrayOutputStream; import java.io.FileInputStream;import java.io.FileOutputStream ...
- JQuery将form表单值转换成json字符串函数
由于后台接口限定,必须要将表单内容转换成json字符串提交,因此写了一个将form表单值转成json字符串的函数. 前提:页面引入了JQuery 下面直接上代码 一.代码 / ...
随机推荐
- CodeForces 546 D. Soldier and Number Game(素数有关)
Description Two soldiers are playing a game. At the beginning first of them chooses a positive integ ...
- CentOS Hadoop安装配置详细
总体思路,准备主从服务器,配置主服务器可以无密码SSH登录从服务器,解压安装JDK,解压安装Hadoop,配置hdfs.mapreduce等主从关系. 1.环境,3台CentOS7,64位,Hadoo ...
- P3414 SAC#1 - 组合数
题目背景 本题由世界上最蒟蒻最辣鸡最撒比的SOL提供. 寂月城网站是完美信息教室的官网.地址:http://191.101.11.174/mgzd . 题目描述 辣鸡蒟蒻SOL是一个傻逼,他居然觉得数 ...
- ListView使用的时候遇到的一些问题
昨天在做项目时,请求服务器的好友动态后,将好友动态和评论显示到界面上,用ListView显示,发现一进这个界面时,listView的适配器的getVIew()方法就会执行6次,后来发现原来是ListV ...
- bluehost 邮箱设置问题
问题描述: e-elitech.com域名,elitechus.com域名均在阿里云注册,en.e-elitech.com解析到bluehost虚拟主机,www.elitechus.com也解析到bl ...
- Spring注解基本解读
在一个类中使用Spring对象,办法如下: 使用注解的形式注入 从ApplicationContext中获取. T t = new ApplicationContext.getBean("x ...
- 9、外观模式(Facade)
外观模式是为了解决类与类之家的依赖关系的,像spring一样,可以将类和类之间的关系配置到配置文件中,而外观模式就是将他们的关系放在一个Facade类中,降低了类类之间的耦合度,该模式中没有涉及到接口 ...
- zabbix Lack of free swap space
Zabbix初始设计是大型公司用于监控服务器集群的,但日常中也用于监控VPS或云主机.后者情况下Zabbix的很多配置和属性就没有经过优化,取决于监控的对象和用途,经常需要对一些Zabbix配置进行调 ...
- MyEclipse快捷键全
Ctrl + Shift + O: 引入imports语句 Ctrl + Shift + T: 打开Open Type查找类文件 Ctrl + Shift + F4: 关闭打开的所有窗口 Ctrl + ...
- StrictMode使用详解
http://hb.qq.com/a/20110914/000054.htm http://www.android100.org/html/201204/25/1097.html http://www ...