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;
}
c++ hex string array 转换 串口常用的更多相关文章
- JavaScript Array和string的转换
Array类可以如下定义: var aValues = new Array(); 如果预先知道数组的长度,可以用参数传递长度 var aValues = new Array(20); -------- ...
- how convert large HEX string to binary array ?
how convert large HEX string to binary I have a string with 14 characters . This is a hex represanta ...
- 【python】bytearray和string之间转换,用在需要处理二进制文件和数据流上
最近在用python搞串口工具,串口的数据流基本读写都要靠bytearray,而我们从pyqt的串口得到的数据都是string格式,那么我们就必须考虑到如何对这两种数据进行转换了,才能正确的对数据收发 ...
- JavaScript string array 数组
Array类可以如下定义: var aValues = new Array(); 如果预先知道数组的长度,可以用参数传递长度 var aValues = new Array(20); -------- ...
- C# 之 将string数组转换到int数组并获取最大最小值
1.string 数组转换到 int 数组 " }; int[] output = Array.ConvertAll<string, int>(input, delegate(s ...
- 转:char*, char[] ,CString, string的转换
转:char*, char[] ,CString, string的转换 (一) 概述 string和CString均是字符串模板类,string为标准模板类(STL)定义的字符串类,已经纳入C++标准 ...
- HTML5 Blob与ArrayBuffer、TypeArray和字符串String之间转换
1.将String字符串转换成Blob对象 //将字符串 转换成 Blob 对象 var blob = new Blob(["Hello World!"], { type: 'te ...
- 分享非常有用的Java程序 (关键代码)(五)---把 Array 转换成 Map
原文:分享非常有用的Java程序 (关键代码)(五)---把 Array 转换成 Map import java.util.Map; import org.apache.commons.lang.Ar ...
- [转] HTML5 Blob与ArrayBuffer、TypeArray和字符串String之间转换
1.将String字符串转换成Blob对象 //将字符串 转换成 Blob 对象 var blob = new Blob(["Hello World!"], { type: 'te ...
随机推荐
- [Google Guava] 3-缓存
原文地址 译文地址 译者:许巧辉 校对:沈义扬 范例 01 LoadingCache<Key, Graph> graphs = CacheBuilder.newBuilder() ...
- python的logging日志模块(二)
晚上比较懒,直接搬砖了. 1.简单的将日志打印到屏幕 import logging logging.debug('This is debug message') logging.info('Thi ...
- django post请求 403错误解决方法
--摘 第一次用Django做项目,遇到了很多问题. 今天遇到的问题是Django在处理post请求时多次出现403错误. 我先描述一下问题出现的环境:我用Django写了一个web服务端,姑且称它为 ...
- [Javascript] Nested generators
To see how to call another generator inside a generator: function* numbers () { ; ; yield* moreNumbe ...
- 在使用Telnet连接localhost时所遇到的问题:出现 ‘telnet’ 不是内部或外部命令,也不是可运行的程序或批处理文件
1.出现 ‘telnet’ 不是内部或外部命令,也不是可运行的程序或批处理文件.原因:因为本机的Telnet客户端默认是关闭的,所以我们要手动打开解决方案:打开控制面板–>程序–>打开或关 ...
- CF103D Time to Raid Cowavans 根号分治+离线
题意: 给定序列 $a,m$ 次询问,每次询问给出 $t,k$. 求 $a_{t}+a_{t+k}+a_{t+2k}+.....a_{t+pk}$ 其中 $t+(p+1)k>n$ 题解: 这种跳 ...
- CF #589 (Div. 2) D. Complete Tripartite 构造
这个 D 还是十分友好的~ 你发现这 $3$ 个集合形成了一个环的关系,所以随意调换顺序是无所谓的. 然后随便让 $1$ 个点成为第 $2$ 集合,那么不与这个点连边的一定也属于第二集合. 然后再随便 ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- 【概率论】4-1:随机变量的期望(The Expectation of a Random Variable Part I)
title: [概率论]4-1:随机变量的期望(The Expectation of a Random Variable Part I) categories: - Mathematic - Prob ...
- elasticsearch sql插件配置(5.0及以上版本)
github官方参考地址:https://github.com/NLPchina/elasticsearch-sql/ 采用 git + node 的方式,所以安装前需要先安装好node,node n ...