Browse Source

update stringutils

disinfection_machine
zhaohe 3 years ago
parent
commit
ea4118ddeb
  1. 25
      core/components/string_util.cpp
  2. 15
      core/components/string_util.hpp

25
core/components/string_util.cpp

@ -97,3 +97,28 @@ bool StringUtil::hexStringToBytes(string in, string delims, vector<uint8_t>& byt
}
return true;
}
/**
* @brief
*
* @param value
* @return string
*/
string StringUtil::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;
if (bit == 0) {
ret = "0" + ret;
} else {
ret = "1" + ret;
}
}
if (remoteZero) {
while (ret[0] == '0') {
ret = ret.substr(1);
}
}
return ret;
}

15
core/components/string_util.hpp

@ -46,6 +46,21 @@ class StringUtil {
* @return false
*/
bool hexStringToBytes(string in, string delims, vector<uint8_t>& byteTable);
/**
* @brief
*
* @param value 0x55
* @param bitCount 8
* @param remoteZero true/false
* @return string 01010101
*
* Demo:
* StringUtil::bytet2Binary(0x55,8,true) ===> 1010101
* StringUtil::bytet2Binary(0x55,8,false) ===> 01010101
*
*/
string bytet2Binary(uint32_t value, int bitCount, bool remoteZero = true);
};
} // namespace core

Loading…
Cancel
Save