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 ...
随机推荐
- getview不执行
<FrameLayout android:layout_width="match_parent" android:layout_height="match_pare ...
- ecstore-lnmp环境下crontab不执行原因
因为lnmp.org默认禁止了proc_open函数,需要开启 开启后 lnmp restart ==== contab还是用crontab -e好,有些用www用户的似乎执行不了
- MySQL服务 - MySQL 5.5编译安装
cmake介绍: MySQL 5.5之后,所有的编译操作都通过cmake进行,使用cmake最大的好处是其独立于源码(out-of-source)的编译功能,即编译工作可以在另一个指定的目录中而非源码 ...
- IOS自定义表格UITableViewCell
在UITableView中,自定义表格,最原始是继承UITableViewCell,然后通过写代码方式去搞,但是这个费事了. 1.在storyboard中 给一个ViewController的tabi ...
- expr命令的一些用法
expr是evaluate expressions的缩写,我的理解它的作用就是用来输出表达式的值. 看下面的几个例子. (1)进行数值运算 $:expr 1 + 2 //'+' 左右两边必须有 ...
- Javascript 技巧集(1)
1. 数组克隆, 使用 slice() 方法 var a1 = [1,2,3,4]; var a2 = a1.slice(); 2. 强制将变量值转化为 bool 类型,前置双感叹号 !! var a ...
- logcat--目录
代码实现获取log日志和logcat使用方法:http://www.apkbus.com/android-128263-1-1.html logcat命令使用方法和查看android系统日志缓冲区内容 ...
- [AIR] 获取U盘,打开U盘
示例: 获取存储卷的方法: package com.controls { import flash.events.StorageVolumeChangeEvent; import flash.file ...
- JE22环境安装配置(JDK/ANT/TOMCAT/ECLIPSE)
文章中不涉及安装的均为解压即可直接使用的 1.安装JDK最新的J2EE安装会默认安装GlassFish,安装Java SDK就足够了,不是非要装JavaEE SDK,因为Tomcat的lib目录下,已 ...
- 3、NASA NIST Big Data Architecture
这篇关于大数据应用的讲解太好了,直接上图.Mattmann_S1P8_ESTF2015 来自为知笔记(Wiz)