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

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. tar 命令详解

    tar命令[root@Linux ~]# tar [-cxtzjvfpPN] 文件与目录 -C 目标目录(注:解压时)参数:-c :建立一个压缩文件的参数指令(create 的意思):-x :解开一个 ...

  2. 浅入浅出EmguCv(一)OpenCv与EmguCv

    最近接触计算机视觉方面的东西,于是准备下手学习opencv,从官网下载windows的安装版,配置环境,一系列步骤走完后,准备按照惯例弄个HelloWord.也就是按照网上的教程,打开了那个图像处理领 ...

  3. Outlook2013 最小化Com加载项

  4. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  5. 源码阅读笔记 - 2 std::vector (2) 关于Allocator Aware Container特性

    所有的STL容器,都保存一个或默认,或由用户提供的allocator的实例,用来提供对象内存分配和构造的方法(除了std::array),这样的容器,被称作Allocator Aware Contai ...

  6. JVM类加载过程

    先不说JVM类加载的原理,先看实例: NormalTest类,包含了一个静态代码块,执行的任务就是打印一句话. /** * 在正常类加载条件下,看静态代码块是否会执行 * @author jianyi ...

  7. JVM实用参数(六) 吞吐量收集器

    在实践中我们发现对于大多数的应用领域,评估一个垃圾收集(GC)算法如何根据如下两个标准: 吞吐量越高算法越好 暂停时间越短算法越好 首先让我们来明确垃圾收集(GC)中的两个术语:吞吐量(through ...

  8. socket listen参数中的backlog 的意义!

    服务器监听时,在每次处理一个客户端的连接时是需要一定时间的,这个时间非常的短(也许只有1ms 或者还不到),但这个时间还是存在的.而这个backlog 存在的意义就是:在这段时间里面除了第一个连接请求 ...

  9. 常见jquery插件

    1.JQuery Tooltipster 2.Bootstrap 3.jQuery UI 1.10

  10. d3安装异常

    使用npm安装D3,发现其工程名和依赖名重复,导致安装异常 http://thisdavej.com/node-newbie-error-npm-refusing-to-install-package ...