linux串口工具 SRT,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

#include

#include

#include // 大写转换

#include

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& data);

//转换一个string到完美string

static string HexStringFormat(const std::string& data=demo_srting);

// string转换为一个数组

static vector 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)

{

vectorhex_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<:iterator> i(format_string.begin(), format_string.end(), e, -1);

regex_token_iterator<: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

{

string one=tmp_get.substr(i,2);

unsigned char value = static_cast(std::stoi(one,0,16));

hex_array.push_back(value);

i++;

}

else

{

string one=tmp_get.substr(i,1);

unsigned char value = static_cast(std::stoi(one,0,16));

hex_array.push_back(value);

}

//break;

}

i++;

}

i++;

}

return HexArrayToString(hex_array);

}

vector string_help::StringToHexArray(const std::string& src)

{

vector 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(std::stoi(one,0,16));

ret.push_back(value);

}

return ret;

}

string string_help::HexArrayToString(const vector& 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"<

string good_str=string_help::HexStringFormat(test_str);

cout << "format string is follow"<

vector 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"<

cout << "input enter to quit"<

while (cin) {

return 0;

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值