Вы не можете выбрать более 25 тем
Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
92 строки
1.9 KiB
C++
92 строки
1.9 KiB
C++
#include <iostream>
|
|
#include <cassert>
|
|
|
|
using namespace std;
|
|
|
|
//char nibble_to_hex(uint8_t i)
|
|
char nibble_to_hex(uint8_t i) //Conversion to hex format
|
|
{
|
|
assert(0x0 <= i && i <= 0xf);
|
|
return "0123456789abcdef"[i];
|
|
}
|
|
|
|
void print_in_hex(uint8_t byte)
|
|
{
|
|
cout << nibble_to_hex(byte >> 4)
|
|
<< nibble_to_hex(byte & 0xf)
|
|
<< endl;
|
|
}
|
|
|
|
const uint8_t* as_bytes(const void* data);
|
|
|
|
void print_in_hex(const void* data, size_t size)
|
|
{
|
|
const uint8_t* bytes = as_bytes(data);
|
|
for (size_t i = 0; i < size; i++)
|
|
{
|
|
print_in_hex(bytes[i]);
|
|
|
|
if ((i + 1) % 16 == 0)
|
|
{
|
|
cout << '\n';
|
|
}
|
|
else
|
|
{
|
|
cout << ' ';
|
|
}
|
|
}
|
|
}
|
|
|
|
const uint8_t* as_bytes(const void* data)
|
|
{
|
|
return reinterpret_cast<const uint8_t*>(data);
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
{
|
|
|
|
assert(nibble_to_hex(0x0) == '0');
|
|
assert(nibble_to_hex(0x1) == '1');
|
|
assert(nibble_to_hex(0x2) == '2');
|
|
assert(nibble_to_hex(0x3) == '3');
|
|
assert(nibble_to_hex(0x4) == '4');
|
|
assert(nibble_to_hex(0x5) == '5');
|
|
assert(nibble_to_hex(0x6) == '6');
|
|
assert(nibble_to_hex(0x7) == '7');
|
|
assert(nibble_to_hex(0x8) == '8');
|
|
assert(nibble_to_hex(0x9) == '9');
|
|
assert(nibble_to_hex(0xa) == 'a');
|
|
assert(nibble_to_hex(0xb) == 'b');
|
|
assert(nibble_to_hex(0xc) == 'c');
|
|
assert(nibble_to_hex(0xd) == 'd');
|
|
assert(nibble_to_hex(0xe) == 'e');
|
|
assert(nibble_to_hex(0xf) == 'f');
|
|
|
|
uint8_t byte = 0xff;
|
|
|
|
uint8_t u8 = 0x42;
|
|
cout << "u8 bytes: ";
|
|
print_in_hex(&u8, sizeof(u8));
|
|
cout << '\n';
|
|
|
|
uint16_t u16 = 0x42;
|
|
cout << "u16 bytes: ";
|
|
print_in_hex(&u16, sizeof(u16));
|
|
cout << '\n';
|
|
|
|
uint32_t u32 = 0x42;
|
|
cout << "u32 bytes: ";
|
|
print_in_hex(&u32, sizeof(u32));
|
|
cout << '\n';
|
|
|
|
|
|
//print_in_hex(byte);
|
|
|
|
|
|
cout << "Press any key for exit... ";
|
|
cin.get();
|
|
return 0;
|
|
}
|