C++怎么吧十六进制字符串转为相应的二进制字符串,比如“FF”转为对应的“11111111”

在线等,求助!!

有个对照表2进制0---15 16进制: 0000(0) 0001(1) 0010 (2) 0011 (3)0100 (4) 0101 (5) 0110 (6) 0111(7) 1000 (8) 1001(9) 1010(A) 1011 (B) 1100(C) 1101(D) 1110 (E) 1111(F)
四位2禁制代表一位16禁制 11111111表示FF
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-04-21
int x,n,ss[256];
char s[256];
n=0; x=0x0af;
while ( x ) { ss[n]=x%2; x/=2; n++; }
for ( i=0;i<n;i++ ) s[n-i-1]=ss[i]+'0';
s[n]=0;
至此,字符串s中存储了x的二进制表示字符串。本回答被提问者和网友采纳
第2个回答  2013-10-26
//#include "stdafx.h"//vc++6.0加上这一行.
#include <iostream>
using namespace std;
void HexToBinary(int x){
if(x) HexToBinary(x/2);
if(x) cout << x%2;
}
void main(void){
int nHex;
cout << "Type a Hexadecimal number(starting from '0x')...\nnHex=";
cin >> hex >> nHex;
HexToBinary(nHex);
cout << endl;
}
第3个回答  2013-10-26

看程序吧。

gnu c++编译。

追问

打不开,txt查看也不行

追答

不可能吧,把文件名改成.cpp试试。
1 #include
2 using namespace std;
3
4 int main() {
5 int n = 0xFFABFFAB;
6 char chword[33];
7 int i;
8
9 for (i=0;i> 31);
1 }
2 chword[32] = 0;
3 cout <<chword << endl;
4 return 0;
5 }

相似回答