Qt通用方法及类库9
函数名
//字节数组转Ascii字符串
static QString byteArrayToAsciiStr(const QByteArray &data);
//16进制字符串转字节数组
static QByteArray hexStrToByteArray(const QString &str);
static char convertHexChar(char ch);
//Ascii字符串转字节数组
static QByteArray asciiStrToByteArray(const QString &str);
//字节数组转16进制字符串
static QString byteArrayToHexStr(const QByteArray &data);
函数体
QString QUIHelper::byteArrayToAsciiStr(const QByteArray &data)
{
QString temp;
int len = data.size();
for (int i = 0; i < len; i++) {
//0x20为空格,空格以下都是不可见字符
char b = data.at(i);
if (0x00 == b) {
temp += QString("\\NUL");
} else if (0x01 == b) {
temp += QString("\\SOH");
} else if (0x02 == b) {
temp += QString("\\STX");
} else if (0x03 == b) {
temp += QString("\\ETX");
} else if (0x04 == b) {
temp += QString("\\EOT");
} else if (0x05 == b) {
temp += QString("\\ENQ");
} else if (0x06 == b) {
temp += QString("\\ACK");
} else if (0x07 == b) {
temp += QString("\\BEL");
} else if (0x08 == b) {
temp += QString("\\BS");
} else if (0x09 == b) {
temp += QString("\\HT");
} else if (0x0A == b) {
temp += QString("\\LF");
} else if (0x0B == b) {
temp += QString("\\VT");
} else if (0x0C == b) {
temp += QString("\\FF");
} else if (0x0D == b) {
temp += QString("\\CR");
} else if (0x0E == b) {
temp += QString("\\SO");
} else if (0x0F == b) {
temp += QString("\\SI");
} else if (0x10 == b) {
temp += QString("\\DLE");
} else if (0x11 == b) {
temp += QString("\\DC1");
} else if (0x12 == b) {
temp += QString("\\DC2");
} else if (0x13 == b) {
temp += QString("\\DC3");
} else if (0x14 == b) {
temp += QString("\\DC4");
} else if (0x15 == b) {
temp += QString("\\NAK");
} else if (0x16 == b) {
temp += QString("\\SYN");
} else if (0x17 == b) {
temp += QString("\\ETB");
} else if (0x18 == b) {
temp += QString("\\CAN");
} else if (0x19 == b) {
temp += QString("\\EM");
} else if (0x1A == b) {
temp += QString("\\SUB");
} else if (0x1B == b) {
temp += QString("\\ESC");
} else if (0x1C == b) {
temp += QString("\\FS");
} else if (0x1D == b) {
temp += QString("\\GS");
} else if (0x1E == b) {
temp += QString("\\RS");
} else if (0x1F == b) {
temp += QString("\\US");
} else if (0x7F == b) {
temp += QString("\\x7F");
} else if (0x5C == b) {
temp += QString("\\x5C");
} else if (0x20 >= b) {
temp += QString("\\x%1").arg(decimalToStrHex((quint8)b));
} else {
temp += QString("%1").arg(b);
}
}
return temp.trimmed();
}
QByteArray QUIHelper::hexStrToByteArray(const QString &str)
{
QByteArray senddata;
int hexdata, lowhexdata;
int hexdatalen = 0;
int len = str.length();
senddata.resize(len / 2);
char lstr, hstr;
for (int i = 0; i < len;) {
hstr = str.at(i).toLatin1();
if (hstr == ' ') {
i++;
continue;
}
i++;
if (i >= len) {
break;
}
lstr = str.at(i).toLatin1();
hexdata = convertHexChar(hstr);
lowhexdata = convertHexChar(lstr);
if ((hexdata == 16) || (lowhexdata == 16)) {
break;
} else {
hexdata = hexdata * 16 + lowhexdata;
}
i++;
senddata[hexdatalen] = (char)hexdata;
hexdatalen++;
}
senddata.resize(hexdatalen);
return senddata;
}
char QUIHelper::convertHexChar(char ch)
{
if ((ch >= '0') && (ch <= '9')) {
return ch - 0x30;
} else if ((ch >= 'A') && (ch <= 'F')) {
return ch - 'A' + 10;
} else if ((ch >= 'a') && (ch <= 'f')) {
return ch - 'a' + 10;
} else {
return (-1);
}
}
QByteArray QUIHelper::asciiStrToByteArray(const QString &str)
{
QByteArray buffer;
int len = str.length();
QString letter;
QString hex;
for (int i = 0; i < len; i++) {
letter = str.at(i);
if (letter == "\\") {
i++;
letter = str.mid(i, 1);
if (letter == "x") {
i++;
hex = str.mid(i, 2);
buffer.append(strHexToDecimal(hex));
i++;
continue;
} else if (letter == "N") {
i++;
hex = str.mid(i, 1);
if (hex == "U") {
i++;
hex = str.mid(i, 1);
if (hex == "L") { //NUL=0x00
buffer.append((char)0x00);
continue;
}
} else if (hex == "A") {
i++;
hex = str.mid(i, 1);
if (hex == "K") { //NAK=0x15
buffer.append(0x15);
continue;
}
}
} else if (letter == "S") {
i++;
hex = str.mid(i, 1);
if (hex == "O") {
i++;
hex = str.mid(i, 1);
if (hex == "H") { //SOH=0x01
buffer.append(0x01);
continue;
} else { //SO=0x0E
buffer.append(0x0E);
i--;
continue;
}
} else if (hex == "T") {
i++;
hex = str.mid(i, 1);
if (hex == "X") { //STX=0x02
buffer.append(0x02);
continue;
}
} else if (hex == "I") { //SI=0x0F
buffer.append(0x0F);
continue;
} else if (hex == "Y") {
i++;
hex = str.mid(i, 1);
if (hex == "N") { //SYN=0x16
buffer.append(0x16);
continue;
}
} else if (hex == "U") {
i++;
hex = str.mid(i, 1);
if (hex == "B") { //SUB=0x1A
buffer.append(0x1A);
continue;
}
}
} else if (letter == "E") {
i++;
hex = str.mid(i, 1);
if (hex == "T") {
i++;
hex = str.mid(i, 1);
if (hex == "X") { //ETX=0x03
buffer.append(0x03);
continue;
} else if (hex == "B") { //ETB=0x17
buffer.append(0x17);
continue;
}
} else if (hex == "O") {
i++;
hex = str.mid(i, 1);
if (hex == "T") { //EOT=0x04
buffer.append(0x04);
continue;
}
} else if (hex == "N") {
i++;
hex = str.mid(i, 1);
if (hex == "Q") { //ENQ=0x05
buffer.append(0x05);
continue;
}
} else if (hex == "M") { //EM=0x19
buffer.append(0x19);
continue;
} else if (hex == "S") {
i++;
hex = str.mid(i, 1);
if (hex == "C") { //ESC=0x1B
buffer.append(0x1B);
continue;
}
}
} else if (letter == "A") {
i++;
hex = str.mid(i, 1);
if (hex == "C") {
i++;
hex = str.mid(i, 1);
if (hex == "K") { //ACK=0x06
buffer.append(0x06);
continue;
}
}
} else if (letter == "B") {
i++;
hex = str.mid(i, 1);
if (hex == "E") {
i++;
hex = str.mid(i, 1);
if (hex == "L") { //BEL=0x07
buffer.append(0x07);
continue;
}
} else if (hex == "S") { //BS=0x08
buffer.append(0x08);
continue;
}
} else if (letter == "C") {
i++;
hex = str.mid(i, 1);
if (hex == "R") { //CR=0x0D
buffer.append(0x0D);
continue;
} else if (hex == "A") {
i++;
hex = str.mid(i, 1);
if (hex == "N") { //CAN=0x18
buffer.append(0x18);
continue;
}
}
} else if (letter == "D") {
i++;
hex = str.mid(i, 1);
if (hex == "L") {
i++;
hex = str.mid(i, 1);
if (hex == "E") { //DLE=0x10
buffer.append(0x10);
continue;
}
} else if (hex == "C") {
i++;
hex = str.mid(i, 1);
if (hex == "1") { //DC1=0x11
buffer.append(0x11);
continue;
} else if (hex == "2") { //DC2=0x12
buffer.append(0x12);
continue;
} else if (hex == "3") { //DC3=0x13
buffer.append(0x13);
continue;
} else if (hex == "4") { //DC2=0x14
buffer.append(0x14);
continue;
}
}
} else if (letter == "F") {
i++;
hex = str.mid(i, 1);
if (hex == "F") { //FF=0x0C
buffer.append(0x0C);
continue;
} else if (hex == "S") { //FS=0x1C
buffer.append(0x1C);
continue;
}
} else if (letter == "H") {
i++;
hex = str.mid(i, 1);
if (hex == "T") { //HT=0x09
buffer.append(0x09);
continue;
}
} else if (letter == "L") {
i++;
hex = str.mid(i, 1);
if (hex == "F") { //LF=0x0A
buffer.append(0x0A);
continue;
}
} else if (letter == "G") {
i++;
hex = str.mid(i, 1);
if (hex == "S") { //GS=0x1D
buffer.append(0x1D);
continue;
}
} else if (letter == "R") {
i++;
hex = str.mid(i, 1);
if (hex == "S") { //RS=0x1E
buffer.append(0x1E);
continue;
}
} else if (letter == "U") {
i++;
hex = str.mid(i, 1);
if (hex == "S") { //US=0x1F
buffer.append(0x1F);
continue;
}
} else if (letter == "V") {
i++;
hex = str.mid(i, 1);
if (hex == "T") { //VT=0x0B
buffer.append(0x0B);
continue;
}
} else if (letter == "\\") {
//如果连着的是多个\\则对应添加\对应的16进制0x5C
buffer.append(0x5C);
continue;
} else {
//将对应的\[前面的\\也要加入
buffer.append(0x5C);
buffer.append(letter.toLatin1());
continue;
}
}
buffer.append(str.mid(i, 1).toLatin1());
}
return buffer;
}
QString QUIHelper::byteArrayToHexStr(const QByteArray &data)
{
QString temp = "";
QString hex = data.toHex();
for (int i = 0; i < hex.length(); i = i + 2) {
temp += hex.mid(i, 2) + " ";
}
return temp.trimmed().toUpper();
}
Qt通用方法及类库9的更多相关文章
- 使用java泛型设计通用方法
泛型是Java SE 1.5的新特性, 泛型的本质是参数化类型, 也就是说所操作的数据类型被指定为一个参数. 因此我们可以利用泛型和反射来设计一些通用方法. 现在有2张表, 一张user表和一张stu ...
- .NET基础架构方法—DataTableToExcel通用方法
p { display: block; margin: 3px 0 0 0; } --> .NET架构基础方法—DataTableToExcel通用方法(NPOI) 今天封装DataTaleTo ...
- DataTable数据赋值给Model通用方法
注:该文属本人原创,今后项目中发现该方法存在BUG会实时更新,转载记得附上原文出处,方便大家获得最新代码. 相信大家在做项目中,经常会根据不同的表new各种不同的Model,当需要对Model进行实例 ...
- 带毫秒的字符转换成时间(DateTime)格式的通用方法
C#自身有更好的方式,Net任意String格式转换为DateTime类型 ====================================================== 原文 ==== ...
- 使用 highchart 绘制柱状图的通用方法与接口
本文给出使用 highchart 绘制柱状图的通用方法与接口, 只要指定相应的数据结构和配置, 就可以直接拿来使用. 一. 数据结构与基本接口 一般绘制图形, 会涉及到较复杂的数据结构, 比如使 ...
- C# 深拷贝通用方法
C#深拷贝通用方法(引用类型的拷贝) /// <summary> /// 深度COPY /// </summary> /// <typeparam name=" ...
- ubuntu下安装与卸载qt的方法
http://blog.csdn.net/huyisu/article/details/24014407 ubuntu下安装与卸载qt的方法 分类: linux 2014-04-18 14:20 18 ...
- List对象排序的通用方法
转自 @author chenchuang import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Me ...
- js添加事件通用方法
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- hibernate学习笔记4---HQL、通用方法的抽取实现
一.通用方法的抽取实现 由于hibernate中对增删改查的一切操作都是面向对象的,所以将增删改查抽取成通用方法,以满足不同的表的增删改查操作,简化jdbc代码. 具体例子如下: package cn ...
随机推荐
- 66.有没有碰到过数组响应丢失(问的是ref和reactive的用法,什么情况下用)
由于vue3使用proxy,对于对象和数组都不能直接整个赋值. 直接赋值丢失了响应性 只有push或者根据索引遍历赋值才可以保留reactive数组的响应性 : 可以使用 toRefs 解决这个问 ...
- LEAP: Learning to Prescribe Effective and Safe Treatment Combinations for Multimorbidity
LEAP: Learning to Prescribe Effective and Safe Treatment Combinations for Multimorbidity Authors: Yu ...
- Ubuntu 22.04 全局快捷键失效问题
安装完 Ubuntu 22.04 后,你有可能会发现系统的快捷键失效了.侧栏用 Win + x 选中程序不可用了.为各种应用程序设置的快捷键也不起作用了. 出现此现象的原因,是因为 Ubuntu 22 ...
- Value error: 'ascii' codec can't decode byte 0xe6 in position 26: ordinal not in range(128) The traceback for the exception was written to the log file
原因:工作空间中有中文编码问题,导致的运行ros异常 解决办法: 1.解决urdf生成异常问题 urdf文件中不允许有中文,所有当输入中文的时候容易出问题,解决方案: ①在根目录下:/opt/ros/ ...
- 题解:CF771A Bear and Friendship Condition
CF771A Bear and Friendship Condition 题解 算法 并查集,图的基本性质 分析 题目意思是,一旦有一些点联通,那么这些点必须两两直接相连.换句话讲,就是图中的每个联通 ...
- 揭秘!Vue3.5响应式重构如何让内存占用减少56%
前言 Vue3.5版本又将响应式给重构了,重构后的响应式系统主要有两部分组成: 双向链表和 版本计数.我们在前两篇文章中我们已经讲过了 双向链表和 版本计数,这篇文章我们来讲讲为什么这次重构能够让内存 ...
- Reviewbot 开源 | 有些 git commit 记录真的不敢恭维, 我推荐每位工程师都常用 git rebase 和 git commit --amend
Reviewbot 是七牛云开源的一个项目,旨在提供一个自托管的代码审查服务, 方便做 code review/静态检查, 以及自定义工程规范的落地. 在日常的编程协作中,Git commit 记录的 ...
- 在哪里可以找到官方的mysql容器图像?
如果您在容器上部署MySQL,那么首要任务之一就是找到正确的镜像. 有一定程度的混乱,尤其是当我们试图帮助部署有问题的人时. 例如,当人们说我使用的是官方的docker镜像- 这到底意味着什么?Doc ...
- 51单片机入门:LED灯控制(01)
第一篇博客,博客园注册很久却一直没有好好利用,今天把以前的文章都删掉,就当开个好头吧. 希望在以后的时间中,自己能够认真.努力.珍惜时间. 零基础入门51单片机 单片机(Microcontroller ...
- java——棋牌类游戏斗地主(webddz1.0)之一
这是本人最近一段时间写的斗地主的java代码,大体框架都实现了,主要缺少,AI的智能算法. 本版本是在singleddz3.0的基础上修改的,修改为了网络版本. 主要修改代码如下: package c ...