qt 几个有点价值的个人创建的程序,字符串转ascii,字符串转hex,hex转字符,时间bcd互转字符串,小数转字符串(直接使用qt的转有bug)(虽然网上有很多c的,但是qt写出来的,更加简洁)
//
//时间转换2021-01-07 12:12:12 =>0x21 0x01 0x07 0x12 0x12 0x12 void Hun::strTimeToBCD(char *tem, QString time) { bool ok; tem[0] = time.mid(2,2).toUInt(&ok,16); tem[1] = time.mid(5,2).toUInt(&ok,16); tem[2] = time.mid(8,2).toUInt(&ok,16); if(time.length()>10){ tem[3] = time.mid(11,2).toUInt(&ok,16); tem[4] = time.mid(14,2).toUInt(&ok,16); tem[5] = time.mid(17,2).toUInt(&ok,16); } } QString Hun::BCDToStrTime(char *tem, int len) { bool ok;QString temstr=""; QString time = QByteArray(tem,len).toHex();//21 02 24 10 12 13 if (len==1)temstr = "20"+time; else if(len==2)temstr = "20"+time.mid(0,2)+"-"+time.mid(2,2); else if(len==3)temstr = "20"+time.mid(0,2)+"-"+time.mid(2,2)+"-"+time.mid(4,2); else if(len==4)temstr = "20"+time.mid(0,2)+"-"+time.mid(2,2)+"-"+time.mid(4,2)+" "+time.mid(6,2)+":00:00"; else if(len==5)temstr = "20"+time.mid(0,2)+"-"+time.mid(2,2)+"-"+time.mid(4,2)+" "+time.mid(6,2)+":"+time.mid(8,2)+":00"; else temstr = "20"+time.mid(0,2)+"-"+time.mid(2,2)+"-"+time.mid(4,2)+" "+time.mid(6,2)+":"+time.mid(8,2)+":"+time.mid(10,2); return temstr; } //u32 数据缩小100倍之后,转字符串小数显示 QString Hun::formatData(u32 *tem, int len) { QString t = "\n"; for(int i=0;i<len;i++){ t+=QString(" %1.%2").arg(tem[i]/100).arg(tem[i]%100); if((i+1)%12==0)t+="\n";if((i+1)%24==0)t+="\n"; } return t; } //hex转字符串 QByteArray有个toHex()函数可以直接转为hex字符串显示 //字符串转hex 在转换时候直接使用字符串的toUInt(&ok,16)函数可以直接显示,其中&ok可以直接使用nullptr代替 //char转uchar不会丧失数据,可以大胆转, 案例如下 QString meterNumber = "0000000100000001"; char bcd[8];bool ok; for(int i=0;i<8;i++){bcd[i] = meterNumber.mid(2*i,2).toUInt(&ok,16);} QString te1 = QByteArray((char*)bcd,8).toHex();//te1=>0000000100000001
//
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构