Browse Source

update stringutils

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

27
core/components/string_util.cpp

@ -96,4 +96,29 @@ bool StringUtil::hexStringToBytes(string in, string delims, vector<uint8_t>& byt
return false; return false;
} }
return true; 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;
}

31
core/components/string_util.hpp

@ -21,24 +21,24 @@ class StringUtil {
/** /**
* @brief new_value="", * @brief new_value="",
*
* @param str
* @param old_value
* @param new_value
* @return string&
*
* @param str
* @param old_value
* @param new_value
* @return string&
*/ */
string& replaceAllDistinct(string& str, const string& old_value, const string& new_value); string& replaceAllDistinct(string& str, const string& old_value, const string& new_value);
/** /**
* @brief HEX字符串成Bytes数组例如 * @brief HEX字符串成Bytes数组例如
*
*
* // string("12,0a,2a,ab") ===> char buf[] = {0x12,0x0a,0x2a,0xab} * // string("12,0a,2a,ab") ===> char buf[] = {0x12,0x0a,0x2a,0xab}
* vector<uint8_t> tobytes; * vector<uint8_t> tobytes;
* hexStringToBytes("12,0a,2a,ab",",",tobytes); * hexStringToBytes("12,0a,2a,ab",",",tobytes);
*
*
* // string("120a2aab") ===> char buf[] = {0x12,0x0a,0x2a,0xab} * // string("120a2aab") ===> char buf[] = {0x12,0x0a,0x2a,0xab}
* vector<uint8_t> tobytes; * vector<uint8_t> tobytes;
* hexStringToBytes("120a2aab","",tobytes); * hexStringToBytes("120a2aab","",tobytes);
*
*
* @param in * @param in
* @param delims * @param delims
* @param byteTable byte数组 * @param byteTable byte数组
@ -46,6 +46,21 @@ class StringUtil {
* @return false * @return false
*/ */
bool hexStringToBytes(string in, string delims, vector<uint8_t>& byteTable); 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 } // namespace core

Loading…
Cancel
Save