From b37d597adb41d300d803616b26dcc2f1afcc7a9e Mon Sep 17 00:00:00 2001 From: VashchishinMV Date: Sun, 6 Apr 2025 18:47:34 +0000 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB(?= =?UTF-8?q?=D0=B0)=20=D0=BD=D0=B0=20'task=5F1'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- task_1 | 104 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 73 insertions(+), 31 deletions(-) diff --git a/task_1 b/task_1 index fe4f573..c2c4839 100644 --- a/task_1 +++ b/task_1 @@ -1,56 +1,98 @@ -#include -#include -#include +#include +#include +#include +#include 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(data); + return reinterpret_cast(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; } \ No newline at end of file