Родитель
b024f6d1d7
Сommit
b37d597adb
@ -1,56 +1,98 @@
|
||||
#include
|
||||
#include
|
||||
#include
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <locale>
|
||||
using namespace std;
|
||||
|
||||
char nibble_to_hex(uint8_t i) {
|
||||
assert(0x0 <= i && i <= 0xf);
|
||||
static const char digits[] = "0123456789abcdef";
|
||||
return digits[i];
|
||||
assert(0x0 <= i && i <= 0xf);
|
||||
static const char digits[] = "0123456789abcdef";
|
||||
return digits[i];
|
||||
}
|
||||
char bit_digit(uint8_t byte, uint8_t bit) {
|
||||
return (byte & (0x1 << bit)) ? '1' : '0';
|
||||
return (byte & (0x1 << bit)) ? '1' : '0';
|
||||
}
|
||||
|
||||
const uint8_t* as_bytes(const void* data) {
|
||||
return reinterpret_cast<const uint8_t*>(data);
|
||||
return reinterpret_cast<const uint8_t*>(data);
|
||||
}
|
||||
|
||||
void print_in_hex(uint8_t byte) {
|
||||
cout << nibble_to_hex(byte >> 4);
|
||||
cout << nibble_to_hex(byte & 0xf);
|
||||
cout << nibble_to_hex(byte >> 4);
|
||||
cout << nibble_to_hex(byte & 0xf);
|
||||
}
|
||||
|
||||
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* 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 << ' ';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_in_binary(uint8_t byte) {
|
||||
|
||||
for (int bit = 7; bit >= 0; bit--) {
|
||||
cout << bit_digit(byte, bit);
|
||||
}
|
||||
for (int bit = 7; bit >= 0; bit--) {
|
||||
cout << bit_digit(byte, bit);
|
||||
}
|
||||
}
|
||||
|
||||
void print_in_binary(const void* data, size_t size) {
|
||||
const uint8_t* bytes = as_bytes(data);
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
print_in_binary(bytes[i]);
|
||||
const uint8_t* bytes = as_bytes(data);
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
print_in_binary(bytes[i]);
|
||||
|
||||
if ((i + 1) % 4 == 0) {
|
||||
cout << '\n';
|
||||
}
|
||||
else {
|
||||
cout << ' ';
|
||||
if ((i + 1) % 4 == 0) {
|
||||
cout << '\n';
|
||||
}
|
||||
else {
|
||||
cout << ' ';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void run_tests() {
|
||||
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');
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
setlocale(LC_ALL, "Russian");
|
||||
uint32_t i;
|
||||
|
||||
cout << "Введите целое число:\n";
|
||||
cin >> i;
|
||||
if (cin.fail()) {
|
||||
cerr << "Ошибка: некорректный ввод. Пожалуйста, введите целое число.\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint32_t test = i;
|
||||
|
||||
cout << "Шестнадцатеричный вывод:\n";
|
||||
print_in_hex(&test, sizeof(test));
|
||||
cout << "\nДвоичный вывод:\n";
|
||||
print_in_binary(&test, sizeof(test));
|
||||
cout << '\n';
|
||||
return 0;
|
||||
}
|
Загрузка…
Ссылка в новой задаче