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 ...
随机推荐
- zhengrui集训笔记2
Day_6 计算几何 点积\Large 点积点积 叉积\Large 叉积叉积 极角\Large 极角极角 < π\piπ :叉积判断 else :atan2 旋转\Large 旋转旋转 左乘第一 ...
- 27、AOP-AOP功能测试
27.AOP-AOP功能测试 AOP : [动态代理]指程序运行期间动态的将某段代码切入到制定方法位置进行运行的编程方式. 导入AOP模块:Spring AOP(spring-aspects) 定义一 ...
- JQuery 实践--让页面动起来
获取和设置元素特性特性属性:是指DOM元素中能够和HTML元素中某个特性对应得上的属性.通常JS特性属性的名称与对应的特性一一匹配,但class <=>className操作特性还是操作属 ...
- php MySQL 查询数据
以下为在MySQL数据库中查询数据通用的 SELECT 语法: SELECT column_name,column_name FROM table_name [WHERE Clause] [LIMIT ...
- CodeChef October Lunchtime 2019 Division 2
HIT: Khaled in HIT 题目描述 Khaled 教练是 HIT(Hag Institute of Technology)一位名师.但是,他有一些困扰. 最近,Khaled 教练正在教一门 ...
- 转:C++何时调用构造函数,何时调用析构函数
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/wjf1997/article/detai ...
- LDA线性分析推广到多分类
感谢皮果提的文章: http://blog.csdn.net/itplus/article/details/12038441 http://blog.csdn.net/itplus/article 皮 ...
- docker-compose ELK+Filebeat查看docker及容器的日志
我目前所在公司开发团队比较小,为集团下面的工厂开发了一套小的系统,跑在一台CentOS服务器上,服务器搭建了docker环境,安装了docker-compose,但在日志处理方面,暂时没有一个好的方法 ...
- js闭包小实验
js闭包小实验 一.总结 一句话总结: 闭包中引用闭包外的变量会使他们常驻内存 function foo() { var i=0; return function () { console.log(i ...
- maven在整合springmvc+hibernate运行时遇到的一些问题
在这里大概记录一下自己在搭建的时候遇到的一些小问题. 1,在获取hibernate的sessionFactory对象时报空指针异常,我的常规配置如下: