1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <map>
#include <string>
#include <cstdlib>
#include <cstdint>
#include <iostream>
bool test_little_endian()
{
uint16_t u = 0xABCD;
if (reinterpret_cast<char *> (&u)[0] == '\xCD' &&
reinterpret_cast<char *> (&u)[1] == '\xAB')
return true;
return false;
}
void show_binary(const char *pc, size_t sz)
{
std::map<char, std::string> nibbles {
{'\x0', "0000"},
{'\x1', "0001"},
{'\x2', "0010"},
{'\x3', "0011"},
{'\x4', "0100"},
{'\x5', "0101"},
{'\x6', "0110"},
{'\x7', "0111"},
{'\x8', "1000"},
{'\x9', "1001"},
{'\xA', "1010"},
{'\xB', "1011"},
{'\xC', "1100"},
{'\xD', "1101"},
{'\xE', "1110"},
{'\xF', "1111"},
};
while (sz--)
{
std::cout << nibbles[(*pc & 0xF0) >> 4] << nibbles[*pc & 0xF] << ' ';
++pc;
}
std::cout << std::endl;
}
#define SHOW_BINARY(v) \
do { \
std::cout << v << '\t'; \
show_binary(reinterpret_cast<const char *> (&v), sizeof v); \
} while (0)
int main()
{
int i1 = -3;
int i2 = -1;
char c1 = 'Y';
char c2 = '\x70';
char a[] = "Hello";
float f = 1000.43f;
if (test_little_endian())
std::cout << "You seem to be using a Little Endian machine, so bytes\n"
" will appear as in reversed order (read bytes from right to left).\n" << std::endl;
SHOW_BINARY(i1);
SHOW_BINARY(i2);
SHOW_BINARY(c1);
SHOW_BINARY(c2);
SHOW_BINARY(a);
SHOW_BINARY(f);
system("PAUSE"); // you may remove this if you're elite
return EXIT_SUCCESS;
}
|