想调优别人的代码,网上搜索一下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字符串的更多相关文章

  1. C#中对象,字符串,dataTable、DataReader、DataSet,对象集合转换成Json字符串方法。

    C#中对象,字符串,dataTable.DataReader.DataSet,对象集合转换成Json字符串方法. public class ConvertJson { #region 私有方法 /// ...

  2. DataTable转换成json字符串

    将DataTable里面的行转换成json字符串方法: #region DataTable转为json /// <summary> /// DataTable转为json /// < ...

  3. Newtonsoft.Json 把对象转换成json字符串

    var resultJson = new { records = rowCount, page = pageindex, //总页数=(总页数+页大小-1)/页大小 total = (rowCount ...

  4. Java将其他数据格式转换成json字符串格式

    package com.wangbo.util; import java.beans.IntrospectionException; import java.beans.Introspector; i ...

  5. json 字符串转换成对象,对象转换成json字符串

    json   字符串转换成对象,对象转换成json字符串 前端: 方法一: parseJSON方法:   [注意jquery版本问题] var str = '{"name":&qu ...

  6. PDF转换成二进制字符串写入 HTTP 输出流

    最近项目需要做电子签章,需要网页打开PDF签章后保存:正好复习哈二进制和流的转换: 文件转换成二进制字符串写入HTTP输出流 protected void Page_Load(object sende ...

  7. JSON对象转换成JSON字符串

    1.问题背景 有一个json对象,需要将其转换成json字符串 JSON.stringify(obj) 2.实现源码 <!DOCTYPE html PUBLIC "-//W3C//DT ...

  8. java 图片转换成base64字符串

    import java.io.ByteArrayOutputStream; import java.io.FileInputStream;import java.io.FileOutputStream ...

  9. JQuery将form表单值转换成json字符串函数

    由于后台接口限定,必须要将表单内容转换成json字符串提交,因此写了一个将form表单值转成json字符串的函数.     前提:页面引入了JQuery          下面直接上代码 一.代码 / ...

随机推荐

  1. MaterialWidgetLibrary 学习

    studio项目地址:https://github.com/keithellis/MaterialWidget 修改后的eclipse项目地址: 修改后的eclipse项目 Demo地址: activ ...

  2. PAT (Advanced Level) 1098. Insertion or Heap Sort (25)

    简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...

  3. HDU 3966 Aragorn's Story 动态树 树链剖分

    Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. “#ifdef __cplusplus extern "C" { #endif”的定义-----C和C++的互相调用

    "#ifdef __cplusplus extern "C" { #endif"的定义 看一些程序的时候老是有 "#ifdef __cplusplus ...

  5. angularjs三级联动

    <div ng-controller="AjaxCtrl"> <h1>AJAX - Oriented</h1> <div> Coun ...

  6. JAVA的RSS处理

    一:什么是RSS RSS(really simple syndication) :网页内容聚合器.RSS的格式是XML.必须符合XML 1.0规范. RSS的作用:订阅BLOG,订阅新闻二:RSS的历 ...

  7. Permission denied: user=xxj, access=WRITE, inode="user":hadoop:supergroup:rwxr-xr-x

    在windows中运行eclipse时报错Permission denied: user=xxj, access=WRITE, inode="user":hadoop:superg ...

  8. post(c),get(r),put(u),delete(d)

    http://whui0110.iteye.com/blog/1682388看这个帖子知道人们常说的幂等意思就是可以重复执行,结果是一样的:帖子中说put(update)是幂等的,其实update v ...

  9. 二分查找javascript

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. POJ 2728 Desert King 最优比率生成树

    Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20978   Accepted: 5898 [Des ...