Hex string convert to Binary String and Vise-Versa(16进制和2进制字符串的相互转换)
这个转换在我们日常的编码中还是很有机会遇到的,这里贴出来和大家分享探讨。
- void pu_hex_to_binary(std::string strHex, std::string &strBinaryResult)
- {
- for ( int i = 0; i < strHex.size(); ++ i ) {
- char chTemp = strHex[i];
- int chHexValue;
- if ( 'F' >= chTemp && chTemp >= 'A' )
- chHexValue = chTemp - 'A' + 10;
- else if ( 'f' >= chTemp && chTemp >= 'a' )
- chHexValue = chTemp - 'a' + 10;
- else
- chHexValue = chTemp - '0';
- std::string strBinary;
- char iBit = 4;
- while( iBit > 0 ) {
- if ( chHexValue % 2 == 0 )
- strBinary.push_back('0');
- else
- strBinary.push_back('1');
- if ( chHexValue > 0 )
- chHexValue >>= 1;
- -- iBit;
- }
- std::reverse(strBinary.begin(), strBinary.end());
- strBinaryResult.append( strBinary );
- }
- }
- void pu_binary_to_hex(std::string strBinary, std::string &strHex )
- {
- int chHexValue = 0;
- strHex.clear();
- for ( int i = 0; i < strBinary.size(); ) {
- std::string strSubBinary;
- if (strBinary.size() - i >= 4) {
- strSubBinary = strBinary.substr(i, 4);
- i += 4;
- }
- else
- {
- strSubBinary = strBinary.substr(i);
- i = strBinary.size();
- }
- std::reverse(strSubBinary.begin(), strSubBinary.end());
- chHexValue = 0;
- for (int j = 0; j < strSubBinary.size(); ++j) {
- char chTemp = strSubBinary [ j ];
- char chBinaryValue = chTemp - '0';
- if (chBinaryValue % 2 == 1)
- chHexValue += 1 << j;
- }
- if (chHexValue < 10)
- strHex.push_back(chHexValue + '0');
- else
- strHex.push_back(chHexValue - 10 + 'A');
- }
- }
Hex string convert to Binary String and Vise-Versa(16进制和2进制字符串的相互转换)的更多相关文章
- How to convert a byte to its binary string representation
How to convert a byte to its binary string representation For example, the bits in a byte B are 1000 ...
- itoa : Convert integer to string
Quote from: http://www.cplusplus.com/reference/cstdlib/itoa/ function Required header : <s ...
- How to: Convert Between Various String Types
This topic demonstrates how to convert various Visual C++ string types into other strings. The str ...
- Binary String Matching
问题 B: Binary String Matching 时间限制: 3 Sec 内存限制: 128 MB提交: 4 解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...
- NYOJ之Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose a ...
- ACM Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- int string convert
C++ int与string的转化 int本身也要用一串字符表示,前后没有双引号,告诉编译器把它当作一个数解释.缺省 情况下,是当成10进制(dec)来解释,如果想用8进制,16进制,怎么办?加上前缀 ...
- Binary String Matching(kmp+str)
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- encode_json 会对给定的Perl的数据结构转换为一个UTF-8 encoded, binary string.
use JSON qw/encode_json decode_json/ ; use Encode; my $data = [ { 'name' => 'Ken' , 'age' => 1 ...
随机推荐
- mysql.my.cnf
[client]port = 3306socket = /tmp/mysql.sock [mysqld]port = 3306socket = /tmp/mysql.sock basedir = /u ...
- Comparable和Comparator实现对象比较
由文生义: 继承Comparable ,表示该类的实例是可以相互比较的; 继承Comparator,表示该类是一个比较器,里面设置了按什么属性比较,list需要按这个比较器里的规则来比较; 使用方法如 ...
- 项目管理-Kick OFF 简称KO
KO的内容包括以下几个过程: 1.项目背景 我们项目在哪里?说过去,做项目之前的“悲惨境地”,明确问题根源在哪里,明白为什么要做这个项目. 2.项目意义.目的与目标 我们项目去哪里?说将来,项目完成之 ...
- 01.Bootstrap入门
Bootstrap介绍: Bootstrap,来自 Twitter,是目前很受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的,它简洁灵活,使得 Web 开发更加 ...
- ios打包出来为pkg的处理方法
Add LSRequiresIPhoneOS YES to your Info.plist The key can be found as Application requires iPhone en ...
- ASP.NET管道
以IIS 6.0为例,在工作进程w3wp.exe中,利用Aspnet_ispai.dll加载.NET运行时(如果.NET运行时尚未加载).IIS 6引入了应用程序池的概念,一个工作进程对应着一个应用程 ...
- mysql on Mac OS
在新买的macbook pro15上安装了mysql,发现2个问题 一个是workbench基本无法正常退出,都要force quit 第二是我正常通过workbench连接后,查看系统log,会发现 ...
- java 无符号byte转换
java中的byte类型是有符号的,值得范围是-128-127 做网络通讯时,接收过来的数据往往都是无符号的byte,值得范围是0-255 因此直接转换时,存储到java显示的值就会有问题 int o ...
- Sorry, but the Android VPN API doesn’t currently allow TAP-based tunnels.
Sorry, but the Android VPN API doesn’t currently allow TAP-based tunnels. Edit .ovpn configfile “dev ...
- 几大最短路径算法比较(Floyd & Dijkstra & Bellman-Ford & SPFA)
几个最短路径算法的比较:Floyd 求多源.无负权边(此处错误?应该可以有负权边)的最短路.用矩阵记录图.时效性较差,时间复杂度O(V^3). Floyd-Warshall算法(Floyd ...