diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/basic_types.cpp | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/src/basic_types.cpp b/src/basic_types.cpp index a6f7033..88640d8 100644 --- a/src/basic_types.cpp +++ b/src/basic_types.cpp @@ -423,9 +423,9 @@ std::string& jau::byteHexString(std::string& dest, const uint8_t value, const bo static constexpr const int radix_max_base = 82; static const std::string radix_symbols = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#%&()+,-.;=@[]^_{}~"; -std::string jau::dec_to_radix(int num, const int base, const int padding_width, const char padding_char) noexcept +std::string jau::dec_to_radix(int32_t num, const int32_t base, const unsigned int padding_width, const char padding_char) noexcept { - if( 0 > num || base > radix_max_base ) { + if( 0 > num || 0 >= base || base > radix_max_base ) { return ""; } std::string res; @@ -435,18 +435,36 @@ std::string jau::dec_to_radix(int num, const int base, const int padding_width, num = quotient.quot; } while ( 0 != num ); - for(int i=res.length(); i<padding_width; ++i) { + for(unsigned int i=res.length(); i<padding_width; ++i) { res.insert(res.begin(), padding_char); } return res; } -int jau::radix_to_dec(const std::string_view& str, const int base) noexcept +std::string jau::dec_to_radix(int64_t num, const int32_t base, const unsigned int padding_width, const char padding_char) noexcept +{ + if( 0 > num || 0 >= base || base > radix_max_base ) { + return ""; + } + std::string res; + do { + std::lldiv_t quotient = std::lldiv(num, (int64_t)base); + res.insert( res.begin(), radix_symbols[ quotient.rem ] ); + num = quotient.quot; + } while ( 0 != num ); + + for(unsigned int i=res.length(); i<padding_width; ++i) { + res.insert(res.begin(), padding_char); + } + return res; +} + +int64_t jau::radix_to_dec(const std::string_view& str, const int base) noexcept { if( base > radix_max_base ) { return -1; } - int res = 0; + int64_t res = 0; for (std::string::size_type pos = 0; pos < str.length(); ++pos) { res = res * base + radix_symbols.find(str[pos]); } |