这个转换在我们日常的编码中还是很有机会遇到的,这里贴出来和大家分享探讨。

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进制字符串的相互转换)的更多相关文章

  1. 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 ...

  2. itoa : Convert integer to string

      Quote from:  http://www.cplusplus.com/reference/cstdlib/itoa/   function   Required header : <s ...

  3. How to: Convert Between Various String Types

      This topic demonstrates how to convert various Visual C++ string types into other strings. The str ...

  4. Binary String Matching

    问题 B: Binary String Matching 时间限制: 3 Sec  内存限制: 128 MB提交: 4  解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...

  5. NYOJ之Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述     Given two strings A and B, whose a ...

  6. ACM Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  7. int string convert

    C++ int与string的转化 int本身也要用一串字符表示,前后没有双引号,告诉编译器把它当作一个数解释.缺省 情况下,是当成10进制(dec)来解释,如果想用8进制,16进制,怎么办?加上前缀 ...

  8. Binary String Matching(kmp+str)

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  9. encode_json 会对给定的Perl的数据结构转换为一个UTF-8 encoded, binary string.

    use JSON qw/encode_json decode_json/ ; use Encode; my $data = [ { 'name' => 'Ken' , 'age' => 1 ...

随机推荐

  1. [linux] linux shell 将解析完毕的文件备份至其他目录

    #!/bin/bash # #将解析完毕的日志备份到别的目录 # #日志目录 rjPath=`ls /home/bgftp/orj` #当前时间戳 cur=`date +%s` #一小时 chtm=3 ...

  2. Install Houdini 12.5 x64 in CentOS 7

    Thanks for Must(QQ ID)'s big help for installing Houdini in CentOS7. - download HOUDINI_FX_V12.5.371 ...

  3. Activity Intent Flags及Task相关属性

    转自http://www.cnblogs.com/lwbqqyumidi/p/3775479.html 今天我们来讲一下Activity的task相关内容. 上次我们讲到Activity的四种启动模式 ...

  4. SQL Server中可能为null的变量逻辑运算的时候要小心

    DECLARE @a int declare @b int IF(@a<>@b) print('@a<>@b') else print('@a=@b') ) print('b& ...

  5. java和h5 canvas德州扑克开发中(一)

    先附上我的德州扑克测试地址 http://120.26.217.116:8080/LxrTexas/texasIndex.html 我和一个朋友的德州扑克历时一个多月开发,目前已经基本可玩. 前端主要 ...

  6. Linux使用ssh公钥实现免密码登录Linux

    ssh 无密码登录要使用公钥与私钥.linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例.有机器A(192.168.1.155),B(192.168.1.181).现想A ...

  7. LAMP_03_Win下Apache+PHP+MySQL整合

    文件: Apache :  首先修改Apache的配置文件,让Apache支持解析PHP文件.Apache配置文件在Apache安装目录的conf目录下的httpd.conf.1. 让Apache可以 ...

  8. 关于litJson的System.InvalidCastException

    最近在做一个Unity3D的项目,用到了litJson库, 它比JavaScript里的JSON解析更加严格, 有时候解析数据的时候会出现类型不对. 比如说 {"data":12} ...

  9. ngrok 配置

    安装git 1.安装git,我安装的是2.6版本,防止会出现另一个错误,安装git所需要的依赖包 yum -y install zlib-devel openssl-devel perl hg cpi ...

  10. springmvc 接受特殊类型字段的处理方法

    springmvc接受前台传入的数据时如果该字段类型无法被封装(如Date),则会出现400 Bad Request错误,解决方法如下. 1.在需要处理的字段前加上注解: @DateTimeForma ...