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.
68 lines
1.9 KiB
68 lines
1.9 KiB
#pragma once
|
|
|
|
#include <algorithm>
|
|
#include <regex>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <vector>
|
|
namespace iflytop {
|
|
namespace core {
|
|
|
|
using namespace std;
|
|
class StringUtils {
|
|
char byteToChar(uint8_t byte);
|
|
bool isLegalHex(char c);
|
|
|
|
public:
|
|
string upper(const string& value);
|
|
string lower(const string& value);
|
|
string bytesToString(const uint8_t* data, size_t size);
|
|
string bytesToString(const char* data, size_t size) { return bytesToString((const uint8_t*)data, size); }
|
|
string bytesToString(const vector<uint8_t>& byteTable) { return bytesToString(byteTable.data(), byteTable.size()); }
|
|
|
|
/**
|
|
* @brief 替换字符串中的字符,如果new_value="",则删除旧字符
|
|
*
|
|
* @param str
|
|
* @param old_value
|
|
* @param new_value
|
|
* @return string&
|
|
*/
|
|
string& replaceAllDistinct(string& str, const string& old_value, const string& new_value);
|
|
/**
|
|
* @brief 转换HEX字符串成Bytes数组例如
|
|
*
|
|
* // string("12,0a,2a,ab") ===> char buf[] = {0x12,0x0a,0x2a,0xab}
|
|
* vector<uint8_t> tobytes;
|
|
* hexStringToBytes("12,0a,2a,ab",",",tobytes);
|
|
*
|
|
* // string("120a2aab") ===> char buf[] = {0x12,0x0a,0x2a,0xab}
|
|
* vector<uint8_t> tobytes;
|
|
* hexStringToBytes("120a2aab","",tobytes);
|
|
*
|
|
* @param in 输入字符串
|
|
* @param delims 分割符
|
|
* @param byteTable 输出的byte数组
|
|
* @return true 转换成功
|
|
* @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:
|
|
* StringUtils::bytet2Binary(0x55,8,true) ===> 1010101
|
|
* StringUtils::bytet2Binary(0x55,8,false) ===> 01010101
|
|
*
|
|
*/
|
|
string bytet2Binary(uint32_t value, int bitCount, bool remoteZero = true);
|
|
};
|
|
|
|
} // namespace core
|
|
} // namespace iflytop
|