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 下面直接上代码 一.代码 / ...
随机推荐
- 4--OC --合成存取器方法
1. 从OC 2.0开始就已经可以自动生成设置函数方法和获取函数方法(统称为存取器方法). 什么是 @property 和 @synthesize ? @property 和 @synthesize ...
- angularjs三级联动
<div ng-controller="AjaxCtrl"> <h1>AJAX - Oriented</h1> <div> Coun ...
- Entity Framework 学习中级篇1—EF支持复杂类型的实现
本节,将介绍如何手动构造复杂类型(ComplexType)以及复杂类型的简单操作. 通常,复杂类型是指那些由几个简单的类型组合而成的类型.比如:一张Customer表,其中有FristName和Las ...
- ERROR: No pool defined. at least one pool section must be specified in config file
root@ubuntu:/opt/php7# /opt/php7/sbin/php-fpm [22-Sep-2015 14:29:00] WARNING: Nothing matches the in ...
- windows下,emacs的配置文件在哪儿?
配置文件_Emacs在你的家目录下"C:/Documents and Settings/username/Application Data". 在Window 7下,配置文件目录在 ...
- C++Builder String 转 char* (转)
源:http://blog.csdn.net/bannico/article/details/7577728 使用C++ Builder 处理字符串经常会遇到兼容性问题. 这次要将String 类型 ...
- Snuke's Subway Trip
すぬけ君の地下鉄旅行 / Snuke's Subway Trip Time limit : 3sec / Memory limit : 256MB Score : 600 points Problem ...
- 生日蛋糕(DFS)
题意: Description 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体. 设从下往上数第i(1 <= i <= M)层蛋糕 ...
- Qt5:Qt程序不在任务拦显示图标
setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint); 回头再写
- 一个action读取另一个action里的session
action 1: private Map session; session.put("projectname_session", request1.getParameter(&q ...