#include <iostream>
#include <sstream>
#include <bitset>
#include <string>
#include <climits>
using namespace std;
const char* hex_char_to_bin(char c)
{
// TODO handle default / error
switch(toupper(c))
{
case '0': return "0000";
case '1': return "0001";
case '2': return "0010";
case '3': return "0011";
case '4': return "0100";
case '5': return "0101";
case '6': return "0110";
case '7': return "0111";
case '8': return "1000";
case '9': return "1001";
case 'A': return "1010";
case 'B': return "1011";
case 'C': return "1100";
case 'D': return "1101";
case 'E': return "1110";
case 'F': return "1111";
}
}
std::string hex_str_to_bin_str(const std::string& hex)
{
// TODO use a loop from <algorithm> or smth
std::string bin;
for(size_t i = 0; i != hex.length(); ++i)
bin += hex_char_to_bin(hex[i]);
return bin;
}
int main() {
char c = 123;
cout << hex << int(c) << endl;
string s = "0x7b";
stringstream ss;
ss << hex << s;
unsigned n;
ss >> n;
bitset<8> b(n);
cout <<b.to_string() << endl;
cout << hex_str_to_bin_str("7b") << endl;
for(char c = 'a'; c <= 'z'; c++){
std::bitset<sizeof(char) * CHAR_BIT> binary(c); //sizeof() returns bytes, not bits!
std::cout << "Letter: " << c << "\t";
std::cout << "Hex: " << std::hex << (int)c << "\t";
std::cout << "Binary: " << binary << std::endl;
}
int x = 123;
const char * xx = reinterpret_cast<const char*>(&x);
cout << xx << endl;
char str[] = {0x61, 0x62, 0x0 ,0x0 ,0x0 ,0x0 ,0x0 ,0x7a};
std::string s2(str);
cout << s2 << endl;
unsigned char u8[] = {0x61, 0x62, 0x0 ,0x0 ,0x0 ,0x0 ,0x0 ,0x7a};
std::string txt(u8,u8+8);
cout << txt << endl;
return 0;
}