c++ hex string array 转换 串口常用
c++ hex string array 转换
效果如下
tset string is follow
0x50 55 0x35 00 10 203040506073031323334ff
format string is follow
5055350010203040506073031323334F0F
now is to convert to a array and then convert to string to show
5055350010203040506073031323334F0F
input enter to quit
#include <iostream>
#include <string>
#include <vector>
#include <algorithm> // 大写转换
#include <regex>
using namespace std;
class string_help
{
public:
string_help();
static bool IsHexNum(char c);
static void deleteAllMark(string &s, const string &mark);
// 假定完美 hex形式的字符串形如 010203040506...FF 全是大写的并且
// 中间无任何分隔符
const static string demo_srting;
// 替换一次
static string replace_all(string& str, const string& old_value, const string& new_value);
// 迭代替换
static string replace_all_distinct(string& str, const string& old_value, const string& new_value);
// hex数组转换为string
static string HexArrayToString(const vector<unsigned char >& data);
//转换一个string到完美string
static string HexStringFormat(const std::string& data=demo_srting);
// string转换为一个数组
static vector<unsigned char> StringToHexArray(const std::string& data=demo_srting);
};
const string string_help::demo_srting="500010203040506073031323334ff";
string string_help:: replace_all_distinct(string& str, const string& old_value, const string& new_value)
{
string::size_type pos=0;
while((pos=str.find(old_value,pos))!= string::npos)
{
str=str.replace(pos,old_value.length(),new_value);
if(new_value.length()>0)
{
pos+=new_value.length();
}
}
return str;
}
//替换2 循环替换,替换后的值也检查并替换 12212 替换12为21----->22211
string string_help::replace_all(string& str, const string& old_value, const string& new_value)
{
string::size_type pos=0;
while((pos=str.find(old_value))!= string::npos)
{
str=str.replace(str.find(old_value),old_value.length(),new_value);
}
return str;
}
void string_help:: deleteAllMark(string &s, const string &mark)
{
size_t nSize = mark.size();
while(1)
{
size_t pos = s.find(mark); // 尤其是这里
if(pos == string::npos)
{
return;
}
s.erase(pos, nSize);
}
}
bool string_help::IsHexNum(char c)
{
if(c>='0' && c<='9') return true;
if(c>='a' && c<='f') return true;
if(c>='A' && c<='F') return true;
return false;
}
string string_help::HexStringFormat(const std::string& data)
{
vector<unsigned char >hex_array;
//1. 转换为大写
string format_string=data;
transform(data.begin(),data.end(),format_string.begin(),::toupper);
//2. 去除0X
replace_all_distinct(format_string,"0X"," ");
replace(format_string.begin(),format_string.end(),',',' ');
replace(format_string.begin(),format_string.end(),',',' ');
regex e("\\s+");
regex_token_iterator<string::iterator> i(format_string.begin(), format_string.end(), e, -1);
regex_token_iterator<string::iterator> end;
while (i != end)
{
//字串处理
string tmp_get=*i;
for(size_t i = 0; i < tmp_get.length(); )
{
if(IsHexNum(tmp_get[i]))
{
if(i+1<tmp_get.length() && IsHexNum(tmp_get[i+1]) )
{
string one=tmp_get.substr(i,2);
unsigned char value = static_cast<unsigned char>(std::stoi(one,0,16));
hex_array.push_back(value);
i++;
}
else
{
string one=tmp_get.substr(i,1);
unsigned char value = static_cast<unsigned char>(std::stoi(one,0,16));
hex_array.push_back(value);
}
//break;
}
i++;
}
i++;
}
return HexArrayToString(hex_array);
}
vector<unsigned char> string_help::StringToHexArray(const std::string& src)
{
vector<unsigned char> ret;
if(src.size()<1)
{
ret.push_back(0x00);
return ret;
}
for (string::size_type i = 0; i < src.size()-1; i+=2)
{
string one=src.substr(i,2);
unsigned char value = static_cast<unsigned char>(std::stoi(one,0,16));
ret.push_back(value);
}
return ret;
}
string string_help::HexArrayToString(const vector<unsigned char >& data)
{
const string hexme = "0123456789ABCDEF";
string ret="";
for(auto it:data)
{
ret.push_back(hexme[(it&0xF0) >>4]);
ret.push_back(hexme[it&0x0F]);
}
return ret;
}
int main()
{
string test_str="0x50 55 0x35 00 10 203040506073031323334ff";
cout << "tset string is follow"<<endl << test_str<<endl;
string good_str=string_help::HexStringFormat(test_str);
cout << "format string is follow"<<endl << good_str<<endl;
vector<unsigned char> text_array= string_help::StringToHexArray(good_str);
string show_srt =string_help::HexArrayToString(text_array);
cout << "now is to convert to a array and then convert to string to show"<<endl << show_srt<<endl;
cout << "input enter to quit"<<endl;
while (cin) {
return 0;
}
return 0;
}