C/C++

Помогите умоляю, прошу!!!

Напишите программу, которая запрашивает число в двоичной системе счисления и выводит его шестнадцатеричную запись. Длина двоичного числа кратна четырём. Каждой шестнадцатеричной цифре соответствуют четыре двоичных цифры:

0 0000 8 1000

1 0001 9 1001

2 0010 A 1010

3 0011 B 1011

4 0100 C 1100

5 0101 D 1101

6 0110 E 1110

7 0111 F 1111

Sample Input 1:


10001111

Sample Output 1:


8F

Sample Input 2:


10010001000100001111100010001000

Sample Output 2:


9110F888

Sample Input 3:


1001101100101001000110011000000000001111

Sample Output 3:


9B2919800F
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

void print16(string str)
{
string TMP;
for(int x = 0; x < str.length(); x += 4)
{
TMP = "";
for(int y = 0; y < 4; y++)
TMP += str[x+y];
if(TMP == "0000"s)
cout << '0';
else if(TMP == "0001"s)
cout << '1';
else if(TMP == "0010"s)
cout << '2';
else if(TMP == "0011"s)
cout << '3';
else if(TMP == "0100"s)
cout << '4';
else if(TMP == "0101"s)
cout << '5';
else if(TMP == "0110"s)
cout << '6';
else if(TMP == "0111"s)
cout << '7';
else if(TMP == "1000"s)
cout << '8';
else if(TMP == "1001"s)
cout << '9';
else if(TMP == "1010"s)
cout << 'A';
else if(TMP == "1011"s)
cout << 'B';
else if(TMP == "1100"s)
cout << 'C';
else if(TMP == "1101"s)
cout << 'D';
else if(TMP == "1110"s)
cout << 'E';
else if(TMP == "1111"s)
cout << 'F';
else
{
cerr << "Error!" << endl;
exit(1);
}
}
cout << endl;
}

int main()
{
string S;
cin >> S;
print16(S);
return 0;
}
Женя Кочкин
Женя Кочкин
91 948
Лучший ответ
#include "iostream"
#include "iomanip"
#include "string"
#include "bitset"
using namespace std;
int main(){
string bin; cout << "Binary number (max 32 digits): "; cin >> bin;
cout << "Hexadecimal: " << hex<< bitset<32>(bin).to_ulong() << endl;}
1000 рублей.
Azamat Ibragimov
Azamat Ibragimov
37 509