Вы не можете выбрать более 25 тем
Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
70 строки
1.6 KiB
C++
70 строки
1.6 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];
|
|
if (i < 10)
|
|
{
|
|
return i + '0';
|
|
}
|
|
else
|
|
{
|
|
return 'a' + (i - 10);
|
|
}
|
|
}
|
|
|
|
uint8_t jun_nibble(uint8_t i) //Extracting the junior Nibble
|
|
{
|
|
return i & 0x0f;
|
|
}
|
|
|
|
uint8_t sen_nibble(uint8_t i) //Extracting the senior Nibble
|
|
{
|
|
return (i & 0xf0) >> 4;
|
|
}
|
|
|
|
void print_in_hex(uint8_t byte)
|
|
{
|
|
//uint8_t upper_nibble = sen_nibble(byte);
|
|
//uint8_t lower_nibble = jun_nibble(byte);
|
|
//cout << nibble_to_hex(upper_nibble) << nibble_to_hex(lower_nibble) << endl;
|
|
cout << nibble_to_hex(sen_nibble(byte)) << nibble_to_hex(jun_nibble(byte)) << endl;
|
|
}
|
|
|
|
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 = 0xce;
|
|
|
|
//cout << "Jun: " << jun_nibble(byte) << endl;
|
|
//cout << "Sen: " << sen_nibble(byte) << endl;
|
|
|
|
print_in_hex(byte);
|
|
|
|
cout << "Press any key for exit... ";
|
|
cin.get();
|
|
return 0;
|
|
}
|