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 ...
随机推荐
- [PHP] - Laravel - Route路由
前言 这里使用的是Laravel 5 PHP Laravel的路由比较强悍,但也正因如此,不统一而容易凌乱.比如在路由中可以直接写方法操作(破坏封装啊) 以下是个人学习的例子,不供参考 路由中的直接方 ...
- AspNetPager控件报错误: Syntax error, unrecognized expression: input#ctl00$ContentPlaceHolder1$Aspnetpager1_input问题解决[摘]
高版本IE,如IE10或者IE11在浏览页面时出现错误: Syntax error, unrecognized expression: input#ctl00$ContentPlaceHolder1$ ...
- nginx下rewrite参数超过9个的解决方法
nginx 在处理多于9个参数的时候,是采用重命名的方法来实现的: /?m?([0-9,]*)h?(\d*)a?([0-9,]*)c?(\d*)s?(x?f?(?P<f>[0-9,]*)/ ...
- 解决ie6下不支持fix属性,模拟固定定位
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- Spring Boot(3)---自定义spring boot starter 问题
1. "Failed to process import candidates for configuration class [com.simple.....]": 主要原因: ...
- BestCoder Round #90 A.Kblack loves flag(随机数生成种子)
A.Kblack loves flag [题目链接]A.Kblack loves flag [题目类型]水题 &题意: kblack喜欢旗帜(flag),他的口袋里有无穷无尽的旗帜. 某天,k ...
- JLINK通过JFLASH烧写bin文件报错处理方法
错误原因:烧写开始地址出错,打开BIN文件后弹出的设置开始地址不正确不能为0 解决措施:用J-FLASH LITE或者将开始地址设置成正确的地址(KEILMDK中IROM1的开始地址
- C++11实现生产者消费者问题
生产者消费者问题是多线程并发中一个非常经典的问题.我在这里实现了一个基于C++11的,单生产者单消费者的版本,供大家参考. #include <windows.h> #include &l ...
- Ubuntu1404 (1)
0.初始设置 (1)开户root账号并重启系统: sudo gedit /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf, 添加greeter-show ...
- Dictionary的几种遍历方法
Dictionary<string, int> list = new Dictionary<string, int>(); list.Add("d", 1) ...