QT 16 binary asii 10 binary conversion

Source: Internet
Author: User

16 binary string to byte array
Static Qbytearray Hexstrtobytearray (QString str)
{
Qbytearray SendData; Send data
int hexdata,lowhexdata;
int hexdatalen = 0;
int len = Str.length (); Ask to send character singing
Senddata.resize (LEN/2); Two-character split
Char lstr,hstr;
for (int i=0; i<len;) {
Hstr=str[i].tolatin1 (); Local characters
if (Hstr = = ") {
i++;
Continue
}
i++;
if (i >= len)
Break
Lstr = Str[i].tolatin1 ();
Hexdata = Converthexchar (HSTR); Convert 16 binary to character type
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);
}

byte array to 16 binary string
Static QString Bytearraytohexstr (Qbytearray data)
{
QString temp= "";
QString Hex=data.tohex ();
for (int i=0;i
Temp+=hex.mid (i,2) + "";
}
Return temp.trimmed (). ToUpper ();
}

16 binary string goto 10 binary
static int strhextodecimal (QString strhex)
{
BOOL OK;
Return Strhex.toint (&ok,16);
}

10 binary string goto 10 binary
static int strdecimaltodecimal (QString strdecimal)
{
BOOL OK;
Return Strdecimal.toint (&ok,10);
}

2 binary string goto 10 binary
static int strbintodecimal (QString strbin)
{
BOOL OK;
Return Strbin.toint (&ok,2);
}

16 binary string to 2 binary string
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 Binary Turn 2 binary string one byte
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 Binary Turn 2 binary string two bytes
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 binary to 16 binary string, complement 0.
static QString decimaltostrhex (int decimal)
{
QString Temp=qstring::number (decimal,16);
if (Temp.length () ==1) {
temp= "0" +temp;
}
return temp;
}


QT 16 binary asii   10 conversion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.