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

  1. void pu_hex_to_binary(std::string strHex, std::string &strBinaryResult)
  2. {
  3. for ( int i = 0; i < strHex.size(); ++ i ) {
  4. char chTemp = strHex[i];
  5. int chHexValue;
  6. if ( 'F' >= chTemp && chTemp >= 'A' )
  7. chHexValue = chTemp - 'A' + 10;
  8. else if ( 'f' >= chTemp && chTemp >= 'a' )
  9. chHexValue = chTemp - 'a' + 10;
  10. else
  11. chHexValue = chTemp - '0';
  12.  
  13. std::string strBinary;
  14. char iBit = 4;
  15. while( iBit > 0 ) {
  16. if ( chHexValue % 2 == 0 )
  17. strBinary.push_back('0');
  18. else
  19. strBinary.push_back('1');
  20. if ( chHexValue > 0 )
  21. chHexValue >>= 1;
  22. -- iBit;
  23. }
  24. std::reverse(strBinary.begin(), strBinary.end());
  25. strBinaryResult.append( strBinary );
  26. }
  27. }
  28.  
  29. void pu_binary_to_hex(std::string strBinary, std::string &strHex )
  30. {
  31. int chHexValue = 0;
  32. strHex.clear();
  33. for ( int i = 0; i < strBinary.size(); ) {
  34. std::string strSubBinary;
  35. if (strBinary.size() - i >= 4) {
  36. strSubBinary = strBinary.substr(i, 4);
  37. i += 4;
  38. }
  39. else
  40. {
  41. strSubBinary = strBinary.substr(i);
  42. i = strBinary.size();
  43. }
  44. std::reverse(strSubBinary.begin(), strSubBinary.end());
  45.  
  46. chHexValue = 0;
  47. for (int j = 0; j < strSubBinary.size(); ++j) {
  48. char chTemp = strSubBinary [ j ];
  49. char chBinaryValue = chTemp - '0';
  50.  
  51. if (chBinaryValue % 2 == 1)
  52. chHexValue += 1 << j;
  53.  
  54. }
  55. if (chHexValue < 10)
  56. strHex.push_back(chHexValue + '0');
  57. else
  58. strHex.push_back(chHexValue - 10 + 'A');
  59. }
  60. }

  

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. mysql.my.cnf

    [client]port = 3306socket = /tmp/mysql.sock [mysqld]port = 3306socket = /tmp/mysql.sock basedir = /u ...

  2. Comparable和Comparator实现对象比较

    由文生义: 继承Comparable ,表示该类的实例是可以相互比较的; 继承Comparator,表示该类是一个比较器,里面设置了按什么属性比较,list需要按这个比较器里的规则来比较; 使用方法如 ...

  3. 项目管理-Kick OFF 简称KO

    KO的内容包括以下几个过程: 1.项目背景 我们项目在哪里?说过去,做项目之前的“悲惨境地”,明确问题根源在哪里,明白为什么要做这个项目. 2.项目意义.目的与目标 我们项目去哪里?说将来,项目完成之 ...

  4. 01.Bootstrap入门

    Bootstrap介绍: Bootstrap,来自 Twitter,是目前很受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的,它简洁灵活,使得 Web 开发更加 ...

  5. ios打包出来为pkg的处理方法

    Add LSRequiresIPhoneOS YES to your Info.plist The key can be found as Application requires iPhone en ...

  6. ASP.NET管道

    以IIS 6.0为例,在工作进程w3wp.exe中,利用Aspnet_ispai.dll加载.NET运行时(如果.NET运行时尚未加载).IIS 6引入了应用程序池的概念,一个工作进程对应着一个应用程 ...

  7. mysql on Mac OS

    在新买的macbook pro15上安装了mysql,发现2个问题 一个是workbench基本无法正常退出,都要force quit 第二是我正常通过workbench连接后,查看系统log,会发现 ...

  8. java 无符号byte转换

    java中的byte类型是有符号的,值得范围是-128-127 做网络通讯时,接收过来的数据往往都是无符号的byte,值得范围是0-255 因此直接转换时,存储到java显示的值就会有问题 int o ...

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

  10. 几大最短路径算法比较(Floyd & Dijkstra & Bellman-Ford & SPFA)

    几个最短路径算法的比较:Floyd 求多源.无负权边(此处错误?应该可以有负权边)的最短路.用矩阵记录图.时效性较差,时间复杂度O(V^3).       Floyd-Warshall算法(Floyd ...