输出内存数据的二进制和十六进制的字符串表示
2016-01-21 11:45
465 查看
场景:
1. 在读取文件或内存时,有时候需要输出那段内存的十六或二进制表示进行分析。
2. 标准的printf没有显示二进制的,而%x显示有最大上限,就是8字节,超过8字节就不行了。
test_binary_hex.cpp
[cpp]
view plaincopyprint?
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <stdint.h>
#include <iostream>
std::string ToBinaryString(const uint8_t* buf,int len)
{
int output_len = len*8;
std::string output;
const char* m[] = {"0","1"};
for(int i = output_len - 1,j = 0; i >=0 ; --i,++j)
{
output.append(m[((uint8_t)buf[j/8] >> (i % 8)) & 0x01],1);
}
return output;
}
std::string ToHexString(const uint8_t* buf,int len,std::string tok = "")
{
std::string output;
char temp[8];
for (int i = 0; i < len; ++i)
{
sprintf(temp,"0x%.2x",(uint8_t)buf[i]);
output.append(temp,4);
output.append(tok);
}
return output;
}
int main(int argc, char const *argv[])
{
printf("0x%.2x\n",1);
uint8_t buf[] = {0x80,0x0f,0x51,0xEE,0xA7};
std::string output = ToBinaryString(buf,2);
std::string output_hex = ToHexString(buf,2,":");
std::cout << output << std::endl;
std::cout << output_hex << std::endl;
assert(!strcmp(output.c_str(),"1000000000001111"));
output = ToBinaryString(buf,3);
std::cout << output << std::endl;
assert(!strcmp(output.c_str(),"100000000000111101010001"));
output = ToBinaryString(buf,4);
assert(!strcmp(output.c_str(),"10000000000011110101000111101110"));
std::cout << output << std::endl;
output = ToBinaryString(buf,5);
assert(!strcmp(output.c_str(),"1000000000001111010100011110111010100111"));
std::cout << output << std::endl;
return 0;
}
输出:
[plain]
view plaincopyprint?
0x01
1000000000001111
0x80:0x0f:
100000000000111101010001
10000000000011110101000111101110
1000000000001111010100011110111010100111
场景:
1. 在读取文件或内存时,有时候需要输出那段内存的十六或二进制表示进行分析。
2. 标准的printf没有显示二进制的,而%x显示有最大上限,就是8字节,超过8字节就不行了。
test_binary_hex.cpp
[cpp]
view plaincopyprint?
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <stdint.h>
#include <iostream>
std::string ToBinaryString(const uint8_t* buf,int len)
{
int output_len = len*8;
std::string output;
const char* m[] = {"0","1"};
for(int i = output_len - 1,j = 0; i >=0 ; --i,++j)
{
output.append(m[((uint8_t)buf[j/8] >> (i % 8)) & 0x01],1);
}
return output;
}
std::string ToHexString(const uint8_t* buf,int len,std::string tok = "")
{
std::string output;
char temp[8];
for (int i = 0; i < len; ++i)
{
sprintf(temp,"0x%.2x",(uint8_t)buf[i]);
output.append(temp,4);
output.append(tok);
}
return output;
}
int main(int argc, char const *argv[])
{
printf("0x%.2x\n",1);
uint8_t buf[] = {0x80,0x0f,0x51,0xEE,0xA7};
std::string output = ToBinaryString(buf,2);
std::string output_hex = ToHexString(buf,2,":");
std::cout << output << std::endl;
std::cout << output_hex << std::endl;
assert(!strcmp(output.c_str(),"1000000000001111"));
output = ToBinaryString(buf,3);
std::cout << output << std::endl;
assert(!strcmp(output.c_str(),"100000000000111101010001"));
output = ToBinaryString(buf,4);
assert(!strcmp(output.c_str(),"10000000000011110101000111101110"));
std::cout << output << std::endl;
output = ToBinaryString(buf,5);
assert(!strcmp(output.c_str(),"1000000000001111010100011110111010100111"));
std::cout << output << std::endl;
return 0;
}
#include <stdlib.h> #include <string.h> #include <stdio.h> #include <assert.h> #include <stdint.h> #include <iostream> std::string ToBinaryString(const uint8_t* buf,int len) { int output_len = len*8; std::string output; const char* m[] = {"0","1"}; for(int i = output_len - 1,j = 0; i >=0 ; --i,++j) { output.append(m[((uint8_t)buf[j/8] >> (i % 8)) & 0x01],1); } return output; } std::string ToHexString(const uint8_t* buf,int len,std::string tok = "") { std::string output; char temp[8]; for (int i = 0; i < len; ++i) { sprintf(temp,"0x%.2x",(uint8_t)buf[i]); output.append(temp,4); output.append(tok); } return output; } int main(int argc, char const *argv[]) { printf("0x%.2x\n",1); uint8_t buf[] = {0x80,0x0f,0x51,0xEE,0xA7}; std::string output = ToBinaryString(buf,2); std::string output_hex = ToHexString(buf,2,":"); std::cout << output << std::endl; std::cout << output_hex << std::endl; assert(!strcmp(output.c_str(),"1000000000001111")); output = ToBinaryString(buf,3); std::cout << output << std::endl; assert(!strcmp(output.c_str(),"100000000000111101010001")); output = ToBinaryString(buf,4); assert(!strcmp(output.c_str(),"10000000000011110101000111101110")); std::cout << output << std::endl; output = ToBinaryString(buf,5); assert(!strcmp(output.c_str(),"1000000000001111010100011110111010100111")); std::cout << output << std::endl; return 0; }
输出:
[plain]
view plaincopyprint?
0x01
1000000000001111
0x80:0x0f:
100000000000111101010001
10000000000011110101000111101110
1000000000001111010100011110111010100111
相关文章推荐
- C#中数据类型的基本了解
- HDU 2476 String painter 区间dp
- 华为OJ题目(十五):求解立方根
- 类的两个属性具有相同名称
- Android6.0 设备Idle状态(二)AlarmManagerService setIdleUntil接口
- h264解码时的AVCDecoderConfigurationRecord 与 CodecPrivateData
- CC2541之SimpleBLEPeripheral程序流程分析 -- 03.App层事务处理
- jquery 获取 outerHtml 包含当前节点本身的代码
- [转载]El Capitan 中 SIP 介绍
- Visual Studio 2015 Owin+MVC+WebAPI+ODataV4+EntityFrawork+Identity+Oauth2.0+AngularJS 1.x 学习笔记
- 弹窗效果处理和改进
- [Androd初级]解决Listview的子项Item的高度无法设置的情节
- 记一次不愉快的bug修复
- 百度地图定位导航错误总结
- 同步请求下载并保存文件
- ldp中ldp_global处理iff接口下标过程
- listview适配器的编写
- Android性能优化典范
- Maven 配置
- 超全!整理常用的iOS第三方资源