You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

124 lines
3.4 KiB

#include "stringutils.hpp"
using namespace iflytop;
using namespace core;
using namespace std;
/***********************************************************************************************************************
* ======================================================private====================================================== *
***********************************************************************************************************************/
char StringUtils::byteToChar(uint8_t byte) {
if (byte < 10) {
return '0' + byte;
} else {
return 'A' + (byte - 10);
}
throw std::out_of_range("byteToChar out of range");
return 'x';
}
bool StringUtils::isLegalHex(char c) {
if (c >= '0' && c <= '9') {
return true;
} else if (c >= 'A' && c <= 'F') {
return true;
} else if (c >= 'a' && c <= 'f') {
return true;
}
return false;
}
/***********************************************************************************************************************
* ======================================================public======================================================= *
***********************************************************************************************************************/
string StringUtils::upper(const string& value) {
string cpy = value;
transform(cpy.begin(), cpy.end(), cpy.begin(), ::toupper);
return cpy;
}
string StringUtils::lower(const string& value) {
string cpy = value;
transform(cpy.begin(), cpy.end(), cpy.begin(), ::tolower);
return cpy;
}
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;
ret += byteToChar(hight4);
ret += byteToChar(low4);
}
return ret;
}
string& StringUtils::replaceAllDistinct(string& str, const string& old_value, const string& new_value) {
for (string::size_type pos(0); pos != string::npos; pos += new_value.length()) {
if ((pos = str.find(old_value, pos)) != string::npos) {
str.replace(pos, old_value.length(), new_value);
} else {
break;
}
}
return str;
}
bool StringUtils::hexStringToBytes(string in, string delims, vector<uint8_t>& byteTable) {
string hexTable;
byteTable.clear();
if (!delims.empty()) {
hexTable = replaceAllDistinct(in, delims, "");
/* code */
} else {
hexTable = in;
}
if (hexTable.length() % 2 != 0) {
// printf("ss\n");
return false;
}
try {
for (unsigned i = 0; i < hexTable.length(); i += 2) {
string hex = hexTable.substr(i, 2);
// printf("ss1 %s\n", hex.c_str());
if (!isLegalHex(hex.c_str()[0])) {
return false;
}
if (!isLegalHex(hex.c_str()[1])) {
return false;
}
int value = std::stoi(hex, 0, 16);
byteTable.push_back((uint8_t)value);
}
} catch (const std::exception& e) {
// printf("ss1\n");
return false;
}
return true;
}
/**
* @brief
*
* @param value
* @return string
*/
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;
if (bit == 0) {
ret = "0" + ret;
} else {
ret = "1" + ret;
}
}
if (remoteZero) {
while (ret[0] == '0') {
ret = ret.substr(1);
}
}
return ret;
}