Qt中字符串转换十六进制
main.cpp 主函数
#include "StringToHex.h" QString str = "ABBCCDD120190E"; QByteArray temp; StringToHex(str, temp); qDebug()<<temp.toHex(); qDebug()<<temp;
StringToHex.h的头文件
char 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); } void StringToHex(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; ) { //char lstr, hstr=str[i].toAscii(); if(hstr == " ") { i++; continue; } i++; if(i >= len) break; lstr = str[i].toAscii(); 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); }
结果显示:
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。