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 ...
随机推荐
- [综] PCA降维
http://blog.json.tw/using-matlab-implementing-pca-dimension-reduction 設有m筆資料, 每筆資料皆為n維, 如此可將他們視為一個mx ...
- AngularJs之$scope对象(作用域)
一.作用域 AngularJs中的$scope对象是模板的域模型,也称为作用域实例.通过为其属性赋值,可以传递数据给模板渲染. 每个$scope都是Scope类的实例,Scope类有很多方法,用于 ...
- 使用log4net 日志输出到数据库MySQL
使用Log4Net的输出日志到MySQL 使用步骤如下: 1. 添加引用Log4Net.dll, mysql.data.dll到工程中 note: mysql.data.dll 版本要高,最好到mys ...
- 新版Retrofit 2可运行例子(解决Could not locate ResponseBody converter for问题)
Retrofit这东西我就不多做解释了,反正最近应用很广,基本都快和OkHttp一起成为安卓的事实网络访问标准框架了. 这么好的一个东西,官网文档实在是不算太好,说的不太清晰.按官网的经常会有“Cou ...
- 进程物理内存远大于Xmx的问题分析
问题描述 最近经常被问到一个问题,”为什么我们系统进程占用的物理内存(Res/Rss)会远远大于设置的Xmx值”,比如Xmx设置1.7G,但是top看到的Res的值却达到了3.0G,随着进程的运行,R ...
- [源码]String StringBuffer StringBudlider(1String部分)
String /** The value is used for character storage. */ private final char value[]; /** Th ...
- 支持ASP.NET MVC
ASP.NET MVC能很方便在后台返回JSON数据,所以与MiniUI进行数据交互非常简单. 1)后台处理: 在MVC控制器中,可以通过方法参数接收数据,也可以通过Request接收更复杂的数据对象 ...
- Ubuntu Java Envrioment
Download Java SDK and Install 1. Download Java SDK from Oracle websit 2.unzip by command line tar -z ...
- ORA-04021:timeout occurred while waiting to lock object
编译某存储过程 ORA-04021 timeout occurred while waiting to lock object stringstringstringstringstring Cause ...
- Tableau地图移动
最近又回归写报表,新的工具使用Tableau,这次要做一个地图,当地图导入之后一直无法能够较好的移动地图,百度也找不到资料. 每次点击一下省份或者利润就是放大或者缩小,很不好移动位置. 研究了一下很简 ...