From 64e8e412a96d903daa4e4262312c0cca043d3e27 Mon Sep 17 00:00:00 2001 From: TimofeevAnM Date: Sun, 26 Jan 2025 12:46:45 +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'lab04=5F1.cpp'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 0 lab04_1.cpp | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) delete mode 100644 README.md create mode 100644 lab04_1.cpp diff --git a/README.md b/README.md deleted file mode 100644 index e69de29..0000000 diff --git a/lab04_1.cpp b/lab04_1.cpp new file mode 100644 index 0000000..a673e49 --- /dev/null +++ b/lab04_1.cpp @@ -0,0 +1,100 @@ +#include +#include +#include + +using namespace std; + +// Преобразование значения от 0 до 15 в шестнадцатеричную цифру +char nibble_to_hex(uint8_t i) { + assert(0x0 <= i && i <= 0xf); + const char digits[] = "0123456789abcdef"; + return digits[i]; +} + +// Извлечение младшего nibble +uint8_t get_lower_nibble(uint8_t byte) { + return byte & 0x0f; // Младшие 4 бита +} + +// Извлечение старшего nibble +uint8_t get_upper_nibble(uint8_t byte) { + return (byte >> 4) & 0x0f; // Старшие 4 бита +} + +// Печать байта в шестнадцатеричном виде +void print_in_hex(uint8_t byte) { + cout << nibble_to_hex(get_upper_nibble(byte)) + << nibble_to_hex(get_lower_nibble(byte)); +} + +// Преобразование void* в uint8_t* +const uint8_t* as_bytes(const void* data) { + return reinterpret_cast(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]); + // Для удобства чтения: пробелы между байтами, по 16 байт на строку. + if ((i + 1) % 16 == 0) { + cout << '\n'; + } else { + cout << ' '; + } + } + cout << '\n'; // Завершение последней строки +} + +// Печать байта в двоичном виде +char bit_digit(uint8_t byte, uint8_t bit) { + return (byte & (0x1 << bit)) ? '1' : '0'; +} + +void print_in_binary(uint8_t byte) { + for (uint8_t bit = 7; bit < 8; bit--) { // С 7 до 0 + 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]); + // Для удобства чтения: пробелы между байтами, по 4 байта на строку. + if ((i + 1) % 4 == 0) { + cout << '\n'; + } else { + cout << ' '; + } + } + cout << '\n'; // Завершение последней строки +} + +// Тестирование функции nibble_to_hex +void test_nibble_to_hex() { + for (uint8_t i = 0; i < 16; i++) { + assert(nibble_to_hex(i) == "0123456789abcdef"[i]); + } +} + +int main() { + // Тестирование функций + test_nibble_to_hex(); + + uint8_t byte = 0xab; + cout << "Hex: "; + print_in_hex(byte); + cout << "Binary: "; + print_in_binary(byte); + + uint32_t u32 = 0x42; + cout << "u32 bytes in hex: "; + print_in_hex(&u32, sizeof(u32)); + cout << "u32 bytes in binary: "; + print_in_binary(&u32, sizeof(u32)); + + return 0; +} \ No newline at end of file