#include<iostream>
using namespace std;
void decimalToBinary(int n) {
int a;
a = n % 2; // 取余
n = n >> 1; //右移一位 相当于除以2
if (0 != n) {
decimalToBinary(n);
}
cout << a;
}
int maineq() {
int i;
cin >> hex >> i;//十进制输入
//cin>>oct>>i;//八进制输入
//cin>>hex>>i;//十六进制输入
cout << "ASCII: " << char(i) << endl;
cout << "OCT: " << oct << i << endl;//输出八进制
cout << "HEX: " << hex << i << endl;//输出十六进制
cout << "DEC: " << dec << i << endl;//输出十进制
cout << "BIN: ";
decimalToBinary(i);//输出二进制
return 0;
}
二进制字符串转为十进制数
void BinToDec(string s, int n)
{
for (int j = s.length() - 1;j >= 0;j--)//二进制字符串转为十进制数
{
int number = s[j] - '0';
n += number*pow(2, s.length() - j - 1);
}
}
十六进制数转为二进制字符串
void HextoTwo(int num,string &ret)
{
int remainder;
char buf[16][5] = { "0000","0001","0010","0011",\
"0100","0101","0110","0111",\
"1000","1001","1010","1011",\
"1100","1101","1110","1111" };
if (0 == num)
return;
remainder = num % 16;
HextoTwo(num >> 4,ret);
ret += buf[remainder];
}