From b50613ad32ed72948db48d462bbbd63fbca4cdff Mon Sep 17 00:00:00 2001 From: zhaohe Date: Mon, 19 Jun 2023 18:54:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20bytet2BinaryHumanReadable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/components/stringutils.cpp | 20 ++++++++++++++++++++ core/components/stringutils.hpp | 1 + 2 files changed, 21 insertions(+) diff --git a/core/components/stringutils.cpp b/core/components/stringutils.cpp index ee473a4..12f2789 100644 --- a/core/components/stringutils.cpp +++ b/core/components/stringutils.cpp @@ -122,6 +122,26 @@ string StringUtils::bytet2Binary(uint32_t value, int bitCount, bool remoteZero) } return ret; } + +string StringUtils::bytet2BinaryHumanReadable(uint32_t value, int bitCount) { + string ret = "("; + + for (int i = 0; i < bitCount; ++i) { + uint32_t bit = value & 0x01; + value = value >> 1; + if (bit == 0) { + ret = "0" + ret; + } else { + ret = "1" + ret; + } + if ((i + 1) % 4 == 0 && i + 1 < bitCount) { + ret = " " + ret; + } + } + ret = ret + ")"; + return ret; +} + string StringUtils::bytet2BinaryBigEnd(uint32_t value, int bitCount, bool remoteZero) { string ret; for (int i = 0; i < bitCount; ++i) { diff --git a/core/components/stringutils.hpp b/core/components/stringutils.hpp index 0cc7dd7..2c67eb7 100644 --- a/core/components/stringutils.hpp +++ b/core/components/stringutils.hpp @@ -62,6 +62,7 @@ class StringUtils { * */ string bytet2Binary(uint32_t value, int bitCount, bool remoteZero = true); + string bytet2BinaryHumanReadable(uint32_t value, int bitCount); string bytet2BinaryBigEnd(uint32_t value, int bitCount, bool remoteZero = true); string escapeSequence(const string& raw_str);