From b1924b555964a91c52c09a93149da17d04d868e8 Mon Sep 17 00:00:00 2001 From: Andrei Solodovnikov Date: Tue, 19 Sep 2023 15:41:07 +0300 Subject: [PATCH] =?UTF-8?q?Cyberconverter.=20=D0=9E=D0=B1=D0=BD=D0=BE?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF=D1=80=D0=BE=D0=B3?= =?UTF-8?q?=D1=80=D0=B0=D0=BC=D0=BC=D1=8B=20=D0=BF=D0=BE=D0=B4=2032=D0=B1?= =?UTF-8?q?=D0=B8=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Индивидуальное задание/cyberconverter.cpp | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/Labs/04. Primitive programmable device/Индивидуальное задание/cyberconverter.cpp b/Labs/04. Primitive programmable device/Индивидуальное задание/cyberconverter.cpp index dce2c1c..ed8dccd 100644 --- a/Labs/04. Primitive programmable device/Индивидуальное задание/cyberconverter.cpp +++ b/Labs/04. Primitive programmable device/Индивидуальное задание/cyberconverter.cpp @@ -12,7 +12,7 @@ void print_help(const std::string program_name) cout << "CYBERcobra program file may contain only comments (starting with \"//\"),\n"; cout << "whitespaces and binary digits '0' or '1'.\n"; cout << "This program will erase this parts from every line and then convert\n"; - cout << "32-bits binary strings into 4 little endian 8-bit strings in hex-format.\n\n"; + cout << "in hex-format.\n\n"; cout << "If output file omitted, the _converted.\n"; cout << "will be produced.\n\n"; cout << "If input file omitted, program.txt will be used.\n\n"; @@ -109,24 +109,19 @@ int main(int argc, char ** argv) cerr << "line " << line_counter << " length is not equal 32 after trimming comments and whitespaces" << endl; return -2; } - // split 32-bits binary line into 4 little-endian hex lines and write them into file - for (size_t i = 0; i < 4; i++) + // Convert into hex lines and write them into file + size_t valid_char_num; + int cur_word = std::stoi(str, &valid_char_num, 2); + if(valid_char_num != 32) { - // For every 8-bit part of 32-bit line get int representation. - // If illegal character found, throw error. - size_t valid_char_num; - string byte_substr = str.substr(8*(3-i), 8); - int cur_byte = std::stoi(byte_substr, &valid_char_num, 2); - if(valid_char_num != 8) - { - cerr << "Illegal character '" << byte_substr.at(valid_char_num) << - "' found in line " << line_counter << ": \"" << str << "\"\n"; - cerr << "Should be only '0' or '1'." << endl; - return -3; - } - char hex_byte_str[3]; + cerr << "Illegal character '" << str.at(valid_char_num) << + "' found in line " << line_counter << ": \"" << str << "\"\n"; + cerr << "Should be only '0' or '1'." << endl; + return -3; + } + char hex_byte_str[9]; // convert int representation into hex string - snprintf(hex_byte_str, 3, "%02x", cur_byte); + snprintf(hex_byte_str, 9, "%08x", cur_word); ofs << hex_byte_str << "\n"; } }