From 2fc631b23cba5f12ad47f685f99f6fbef4885c8a Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Tue, 22 Mar 2022 23:58:44 +0100 Subject: Move FormatHex and ParseHex to zencore/string --- zencore/string.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'zencore/string.cpp') diff --git a/zencore/string.cpp b/zencore/string.cpp index ad6ee78fc..f116ac477 100644 --- a/zencore/string.cpp +++ b/zencore/string.cpp @@ -335,6 +335,33 @@ NiceNumGeneral(uint64_t Num, std::span Buffer, NicenumFormat Format) } } +bool +ParseHex(const std::string HexString, uint32_t& OutValue) +{ + if (HexString.length() != 8) + { + return false; + } + OutValue = strtoul(HexString.data(), 0, 16); + return true; +} + +static const char HexLUT[] = "0123456789abcdef"; + +void +FormatHex(uint32_t Value, char OutBlockHexString[9]) +{ + OutBlockHexString[0] = HexLUT[(Value >> 28) & 0xf]; + OutBlockHexString[1] = HexLUT[(Value >> 24) & 0xf]; + OutBlockHexString[2] = HexLUT[(Value >> 20) & 0xf]; + OutBlockHexString[3] = HexLUT[(Value >> 16) & 0xf]; + OutBlockHexString[4] = HexLUT[(Value >> 12) & 0xf]; + OutBlockHexString[5] = HexLUT[(Value >> 8) & 0xf]; + OutBlockHexString[6] = HexLUT[(Value >> 4) & 0xf]; + OutBlockHexString[7] = HexLUT[(Value >> 0) & 0xf]; + OutBlockHexString[8] = 0; +} + size_t NiceNumToBuffer(uint64_t Num, std::span Buffer) { -- cgit v1.2.3