/****************************************************************************** * This program illustrates conversion of numbers to different bases. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ #undef NDEBUG #include "Utils.hpp" #include #include #include #include #include #include #include std::locale utf8loc(std::locale(), new std::codecvt_utf8); using namespace Utils; using namespace std; static const vector SYMBOLS = {L"0", L"1", L"2", L"3", L"4", L"5", L"6", L"7", L"8", L"9", L"A", L"B", L"C", L"D", L"E", L"F"}; wstring convert_to_base(int i, int base) noexcept { wstring result = L""; if (i == 0) { return L"0"; } int x = i; while (x > 0) { int const DIGIT = x % base; result = SYMBOLS[DIGIT] + result; x = x / base; } return result; } static int const SUBSCRIPT0 = 0x2080; wstring compose_subscript(int base) noexcept { wstring result = L""; int x = base; while (x > 0) { result = wstring(1, wchar_t(SUBSCRIPT0 + (x % 10))) + result; x = x / 10; } return result; } int main(int argc, char **argv) { setlocale(LC_ALL, "en_US.UTF-8"); wcout.imbue(utf8loc); wcin.imbue(utf8loc); if (argc != 3) { wcout << fmt::format(L"Syntax: {0:s} max base", Utils::UTF8_to_wstring(argv[0])) << endl; exit(1); } int const MAX = Utils::stoiWithDefault(Utils::UTF8_to_wstring(argv[1]), 64); int const BASE = Utils::stoiWithDefault(Utils::UTF8_to_wstring(argv[2]), 16); for (int i = 0; i <= MAX; ++i) { wcout << fmt::format(L"{0:d} = {2:s}{1:s}", i, compose_subscript(BASE), convert_to_base(i, BASE)) << endl; } return 0; }