您好,登錄后才能下訂單哦!
//16進制字符串轉字節數組
static QByteArray HexStrToByteArray(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[i].toLatin1(); //本地字符
if(hstr == ' ') {
i++;
continue;
}
i++;
if(i >= len)
break;
lstr = str[i].toLatin1();
hexdata = ConvertHexChar(hstr); //將16進制轉換字符型
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;
}
static 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);
}
//字節數組轉16進制字符串
static QString ByteArrayToHexStr(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();
}
//16進制字符串轉10進制
static int StrHexToDecimal(QString strHex)
{
bool ok;
return strHex.toInt(&ok,16);
}
//10進制字符串轉10進制
static int StrDecimalToDecimal(QString strDecimal)
{
bool ok;
return strDecimal.toInt(&ok,10);
}
//2進制字符串轉10進制
static int StrBinToDecimal(QString strBin)
{
bool ok;
return strBin.toInt(&ok,2);
}
//16進制字符串轉2進制字符串
static QString StrHexToStrBin(QString strHex)
{
uchar decimal=StrHexToDecimal(strHex);
QString bin=QString::number(decimal,2);
uchar len=bin.length();
if (len<8){
for (int i=0;i<8-len;i++){
bin="0"+bin;
}
}
return bin;
}
//10進制轉2進制字符串一個字節
static QString DecimalToStrBin1(int decimal)
{
QString bin=QString::number(decimal,2);
uchar len=bin.length();
if (len<=8){
for (int i=0;i<8-len;i++){
bin="0"+bin;
}
}
return bin;
}
//10進制轉2進制字符串兩個字節
static QString DecimalToStrBin2(int decimal)
{
QString bin=QString::number(decimal,2);
uchar len=bin.length();
if (len<=16){
for (int i=0;i<16-len;i++){
bin="0"+bin;
}
}
return bin;
}
//10進制轉16進制字符串,補零.
static QString DecimalToStrHex(int decimal)
{
QString temp=QString::number(decimal,16);
if (temp.length()==1){
temp="0"+temp;
}
return temp;
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。