[C++] break into nibbles, use std::map to decode[C++] break into nibbles, use std::map to decode

⚙️

ABCD Message

Week 21, 2026

#include "allheaders.hpp" #include "main.hpp" using namespace std; //----------------------[GLOBALS] vectorwholeStr; vectorbcdStr; string bcdString, binString; fstream infile, wholefile, temp; void init_BCD_map(unordered_map &BCDMap); void bcdToText(string bcdCode); void splitStr(const string& str); // PROGRAM 52 - week 21 -------------------------------------[BEGIN LOGIC]------ int main() { try { temp.open("tempfile", ios::in |ios::out | ios::trunc);//[split into nibbles] wholefile.open("challenge21.bin", ios::in | ios::binary);//[challenge text] while(getline(wholefile, binString)) { wholeStr.push_back(binString); } int wholesize = wholeStr[0].length(); string getSplitStr = wholeStr[0]; splitStr(getSplitStr); temp.close(); infile.open("tempfile", ios::in | ios::out);//[reopen nibbles file] while(getline(infile, bcdString)) { bcdStr.push_back(bcdString); } int size = bcdStr.size(); for(int i = 0; i < size; i++) { bcdToText(bcdStr[i]); } // PROGRAM 52 - week 21 ---------------------------------------[END LOGIC]------ }// end of try catch (const runtime_error& exception) { cout << "Msg: " << exception.what() << "\n"; } cout << "\n"; return 0; } //-------------------------------------------------------[FUNCTION DEFINITIONS] void splitStr(const string& str) { for (size_t i = 0; i < str.length(); i += 4) { temp << str.substr(i, 4) << "\n"; } } //------------------------------------------------------------------------------ void bcdToText(string bcdCode) { map BCDMap = { {"0000", "0"}, {"0001", "1"}, {"0010", "2"}, {"0011", "3"}, {"0100", "4"}, {"0101", "5"}, {"0110", "6"}, {"0111", "7"}, {"1000", "8"}, {"1001", "9"}, {"1010", "A"}, {"1011", "B"}, {"1100", "C"}, {"1101", "D"}, {"1110", "E"}, {"1111", "."} }; size_t pos = 0; while ((pos = bcdCode.find(' ')) != string::npos) { string token = bcdCode.substr(0, pos); if (BCDMap.find(token) != BCDMap.end()) { cout << BCDMap[token]; } else { cout << " "; } bcdCode.erase(0, pos + 1); } cout << BCDMap[bcdCode]; return; }