diff options
| author | Cerapter <cerap@protonmail.com> | 2018-08-19 09:20:16 +0200 |
|---|---|---|
| committer | Cerapter <cerap@protonmail.com> | 2018-08-19 09:20:16 +0200 |
| commit | e6eace9a39fe4d0f0755d86686650ccfdff81982 (patch) | |
| tree | 91dcdf4bc08e3f5e5ac312f46f0e4bdd0edcdc71 | |
| parent | c316e81e0c28a5f62e55f4a727f0ca152ab9874b (diff) | |
Better INT to HEX conversion.
Reimplementation of `4e96a41b4ee6bbc920b7c5a5ec555d6d14e65b18`,
`bb0b767ba40d189b97ffe371ab063c5380609b0c` and
`e36dae20b7d1baba912c55ec55b82a380c4973da` from old origin.
| -rw-r--r-- | hex_functions.cpp | 35 | ||||
| -rw-r--r-- | hex_functions.h | 6 |
2 files changed, 13 insertions, 28 deletions
diff --git a/hex_functions.cpp b/hex_functions.cpp index 9db2b0a7..d22719f4 100644 --- a/hex_functions.cpp +++ b/hex_functions.cpp @@ -4,7 +4,7 @@ namespace omni { - char halfword_to_hex_char(unsigned int input) + /*char halfword_to_hex_char(unsigned int input) { if (input > 127) return 'F'; @@ -46,38 +46,19 @@ namespace omni default: return 'F'; } - } + }*/ std::string int_to_hex(unsigned int input) { if (input > 255) return "FF"; - std::bitset<8> whole_byte(input); - //240 represents 11110000, our needed bitmask - uint8_t left_mask_int = 240; - std::bitset<8> left_mask(left_mask_int); - std::bitset<8> left_halfword((whole_byte & left_mask) >> 4); - //likewise, 15 represents 00001111 - uint8_t right_mask_int = 15; - std::bitset<8> right_mask(right_mask_int); - std::bitset<8> right_halfword((whole_byte & right_mask)); - - unsigned int left = left_halfword.to_ulong(); - unsigned int right = right_halfword.to_ulong(); - - //std::cout << "now have have " << left << " and " << right << '\n'; - - char a = halfword_to_hex_char(left); - char b = halfword_to_hex_char(right); - - std::string left_string(1, a); - std::string right_string(1, b); - - std::string final_byte = left_string + right_string; - - //std::string final_byte = halfword_to_hex_char(left) + "" + halfword_to_hex_char(right); + std::stringstream stream; + stream << std::setfill('0') << std::setw(sizeof(char)*2) + << std::hex << input; + std::string result(stream.str()); + std::transform(result.begin(), result.end(), result.begin(), ::toupper); - return final_byte; + return result; } } //namespace omni diff --git a/hex_functions.h b/hex_functions.h index 47d9466b..20c5cad6 100644 --- a/hex_functions.h +++ b/hex_functions.h @@ -4,10 +4,14 @@ #include <bitset> #include <cstdint> #include <iostream> +#include <algorithm> +#include <string> +#include <iomanip> +#include <sstream> namespace omni { - char halfword_to_hex_char(unsigned int input); + //char halfword_to_hex_char(unsigned int input); std::string int_to_hex(unsigned int input); } |
