From ff65e5549835f4187d8ef6be14528ed72ae5f632 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Wed, 22 Mar 2023 10:45:55 +0800 Subject: [PATCH] update stringutils.cpp --- core/components/stringutils.cpp | 26 ++++++++++++++++++++++---- core/components/stringutils.hpp | 1 + 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/core/components/stringutils.cpp b/core/components/stringutils.cpp index f5ccdd0..becd10d 100644 --- a/core/components/stringutils.cpp +++ b/core/components/stringutils.cpp @@ -44,7 +44,7 @@ string StringUtils::bytesToString(const uint8_t* data, size_t size) { string ret; for (unsigned i = 0; i < size; ++i) { uint8_t hight4 = data[i] >> 4 & 0x0f; - uint8_t low4 = data[i] >> 0 & 0x0f; + uint8_t low4 = data[i] >> 0 & 0x0f; ret += byteToChar(hight4); ret += byteToChar(low4); } @@ -108,7 +108,7 @@ string StringUtils::bytet2Binary(uint32_t value, int bitCount, bool remoteZero) string ret; for (int i = 0; i < bitCount; ++i) { uint32_t bit = value & 0x01; - value = value >> 1; + value = value >> 1; if (bit == 0) { ret = "0" + ret; } else { @@ -116,9 +116,27 @@ string StringUtils::bytet2Binary(uint32_t value, int bitCount, bool remoteZero) } } if (remoteZero) { - while (ret[0] == '0') { - ret = ret.substr(1); + while (ret.length() > 0 && ret[0] == '0') { + ret = ret.substr(1, ret.length() - 1); } } return ret; } +string StringUtils::bytet2BinaryBigEnd(uint32_t value, int bitCount, bool remoteZero) { + string ret; + for (int i = 0; i < bitCount; ++i) { + uint32_t bit = value & 0x01; + value = value >> 1; + if (bit == 0) { + ret = ret + "0"; + } else { + ret = ret + "1"; + } + } + if (remoteZero) { + while (ret.length() > 0 && ret[ret.length() - 1] == '0') { + ret = ret.substr(0, ret.length() - 1); + } + } + return ret; +} \ No newline at end of file diff --git a/core/components/stringutils.hpp b/core/components/stringutils.hpp index 68dbb95..1b92331 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 bytet2BinaryBigEnd(uint32_t value, int bitCount, bool remoteZero = true); }; } // namespace core