From fb6fd1b3c25fd9c2bc9b8abb97fd150bc4ce219c Mon Sep 17 00:00:00 2001 From: Kevin Kelley Date: Thu, 22 Nov 2018 08:02:25 +0800 Subject: add a 'readable' format for large hex values --- libdevcore/CommonData.h | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'libdevcore/CommonData.h') 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 -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::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::type>(datum); + } + return (_prefix == HexPrefix::Add) ? "0x" + ret.str() : ret.str(); } -- cgit v1.2.3 From 3a378eae1a57683d0f31ab54c122b2a5c6a7c8bb Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 5 Dec 2018 20:34:27 +0100 Subject: Restrict toHex to `bytes`. --- libdevcore/CommonData.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'libdevcore/CommonData.h') diff --git a/libdevcore/CommonData.h b/libdevcore/CommonData.h index 4118907c..0a8c37d2 100644 --- a/libdevcore/CommonData.h +++ b/libdevcore/CommonData.h @@ -61,12 +61,11 @@ enum class HexCase /// 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 -std::string toHex(T const& _data, int _w = 2, HexPrefix _prefix = HexPrefix::DontAdd, HexCase _case = HexCase::Lower) +inline std::string toHex(bytes const& _data, int _w = 2, HexPrefix _prefix = HexPrefix::DontAdd, HexCase _case = HexCase::Lower) { std::ostringstream ret; int rix = _data.size() - 1; - for (auto datum: _data) + for (uint8_t c: _data) { // switch hex case every four hexchars auto hexcase = std::nouppercase; @@ -76,7 +75,7 @@ std::string toHex(T const& _data, int _w = 2, HexPrefix _prefix = HexPrefix::Don hexcase = (rix-- & 2) == 0 ? std::nouppercase : std::uppercase; ret << std::hex << hexcase << std::setfill('0') << std::setw(_w) - << +static_cast::type>(datum); + << size_t(c); } return (_prefix == HexPrefix::Add) ? "0x" + ret.str() : ret.str(); -- cgit v1.2.3 From e8455c2a6d33abbebfe678fdaa1728311838e21a Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 5 Dec 2018 20:35:45 +0100 Subject: Move toHex implementation to cpp file. --- libdevcore/CommonData.h | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) (limited to 'libdevcore/CommonData.h') diff --git a/libdevcore/CommonData.h b/libdevcore/CommonData.h index 0a8c37d2..6da2b292 100644 --- a/libdevcore/CommonData.h +++ b/libdevcore/CommonData.h @@ -61,25 +61,7 @@ enum class HexCase /// 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" -inline std::string toHex(bytes const& _data, int _w = 2, HexPrefix _prefix = HexPrefix::DontAdd, HexCase _case = HexCase::Lower) -{ - std::ostringstream ret; - int rix = _data.size() - 1; - for (uint8_t c: _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) - << size_t(c); - } - - return (_prefix == HexPrefix::Add) ? "0x" + ret.str() : ret.str(); -} +std::string toHex(bytes const& _data, int _w = 2, HexPrefix _prefix = HexPrefix::DontAdd, HexCase _case = HexCase::Lower); /// Converts a (printable) ASCII hex character into the correspnding integer value. /// @example fromHex('A') == 10 && fromHex('f') == 15 && fromHex('5') == 5 -- cgit v1.2.3 From bc6ddbdd09860542061baed8df993cb3dafaa406 Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 5 Dec 2018 22:11:31 +0100 Subject: Remove `w` parameter for toHex. --- libdevcore/CommonData.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'libdevcore/CommonData.h') diff --git a/libdevcore/CommonData.h b/libdevcore/CommonData.h index 6da2b292..7c59c505 100644 --- a/libdevcore/CommonData.h +++ b/libdevcore/CommonData.h @@ -61,7 +61,7 @@ enum class HexCase /// 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" -std::string toHex(bytes const& _data, int _w = 2, HexPrefix _prefix = HexPrefix::DontAdd, HexCase _case = HexCase::Lower); +std::string toHex(bytes const& _data, HexPrefix _prefix = HexPrefix::DontAdd, HexCase _case = HexCase::Lower); /// Converts a (printable) ASCII hex character into the correspnding integer value. /// @example fromHex('A') == 10 && fromHex('f') == 15 && fromHex('5') == 5 @@ -153,7 +153,7 @@ inline std::string formatNumber(bigint const& _value) if (_value < 0) return "-" + formatNumber(-_value); if (_value > 0x1000000) - return toHex(toCompactBigEndian(_value), 2, HexPrefix::Add); + return toHex(toCompactBigEndian(_value), HexPrefix::Add); else return _value.str(); } @@ -161,7 +161,7 @@ inline std::string formatNumber(bigint const& _value) inline std::string formatNumber(u256 const& _value) { if (_value > 0x1000000) - return toHex(toCompactBigEndian(_value), 2, HexPrefix::Add); + return toHex(toCompactBigEndian(_value), HexPrefix::Add); else return _value.str(); } -- cgit v1.2.3