diff options
author | Kevin Kelley <kevin@kelleysoft.com> | 2018-11-22 08:02:25 +0800 |
---|---|---|
committer | chriseth <chris@ethereum.org> | 2018-12-06 05:15:02 +0800 |
commit | fb6fd1b3c25fd9c2bc9b8abb97fd150bc4ce219c (patch) | |
tree | 654e692585f825a6ca1ea50c1e27b06b7f2107e1 /libdevcore/CommonData.h | |
parent | d3c8ba00ac00426b256998a678a8d4bfbf0acd83 (diff) | |
download | dexon-solidity-fb6fd1b3c25fd9c2bc9b8abb97fd150bc4ce219c.tar.gz dexon-solidity-fb6fd1b3c25fd9c2bc9b8abb97fd150bc4ce219c.tar.zst dexon-solidity-fb6fd1b3c25fd9c2bc9b8abb97fd150bc4ce219c.zip |
add a 'readable' format for large hex values
Diffstat (limited to 'libdevcore/CommonData.h')
-rw-r--r-- | libdevcore/CommonData.h | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/libdevcore/CommonData.h b/libdevcore/CommonData.h index fedd3af2..4118907c 100644 --- a/libdevcore/CommonData.h +++ b/libdevcore/CommonData.h @@ -50,16 +50,35 @@ enum class HexPrefix DontAdd = 0, Add = 1, }; + +enum class HexCase +{ + Lower = 0, + Upper = 1, + Mixed = 2, +}; + /// Convert a series of bytes to the corresponding string of hex duplets. /// @param _w specifies the width of the first of the elements. Defaults to two - enough to represent a byte. /// @example toHex("A\x69") == "4169" template <class T> -std::string toHex(T const& _data, int _w = 2, HexPrefix _prefix = HexPrefix::DontAdd) +std::string toHex(T const& _data, int _w = 2, HexPrefix _prefix = HexPrefix::DontAdd, HexCase _case = HexCase::Lower) { std::ostringstream ret; - unsigned ii = 0; - for (auto i: _data) - ret << std::hex << std::setfill('0') << std::setw(ii++ ? 2 : _w) << (int)(typename std::make_unsigned<decltype(i)>::type)i; + int rix = _data.size() - 1; + for (auto datum: _data) + { + // switch hex case every four hexchars + auto hexcase = std::nouppercase; + if (_case == HexCase::Upper) + hexcase = std::uppercase; + else if (_case == HexCase::Mixed) + hexcase = (rix-- & 2) == 0 ? std::nouppercase : std::uppercase; + + ret << std::hex << hexcase << std::setfill('0') << std::setw(_w) + << +static_cast<typename std::make_unsigned<decltype(datum)>::type>(datum); + } + return (_prefix == HexPrefix::Add) ? "0x" + ret.str() : ret.str(); } |