Some QT character conversion

Source: Internet
Author: User

Note: In windows, "we" is written in the IE Address Bar"

The message sent to the server becomes "% Ce % D2 % C3 % C7 ",

Is the form of converting gb2312 to URL percent code. In ubuntu, "we" is written in the Firefox address bar and sent to the server as "% E6 % 88% E4 % BB % ac ",

Is the form of converting UTF-8 to URL percent code

The following is a summary of the conversion code: (my environment: ubuntu10.10 qt4.6.2) 01

// Ubuntu Firefox http get "we" % E6 % 88% 91% E4 % BB % AC UTF-802

// Windows ie http get "" % Ce % D2 % C3 % C7 gb231203

Char * s = "0x us ";

// UTF-8 char *

Qstring strt1 = "0x % Ce % D2 % C3 % C7 ";

// Gb2312 URL percent code 05

Qbytearray bat1, bat2, bat3, bat4;

Percentencoding2bytearray (strt1, bat1); // self-written function, since qurl: frompercentencoding only applies to URL encoding of the UTF-8

Strt1 = qtextcodec: codecforname ("gb2312")-> tounicode (bat1); // gb2312's qbytearray convert to qstring Unicode

Bat2 = qtextcodec: codecforname ("gb2312")-> fromunicode (strt1); // qstring Unicode convert to gb2312 qbytearray

Qstring strt2, strt3, strt4, strt5;

Strt2 = qtextcodec: codecforname ("UTF-8")-> tounicode ("0x we"); // UTF-8 char * to Unicode qstring

Bat3 = qtextcodec: codecforname ("UTF-8")-> fromunicode (strt2); // Unicode qstring to UTF-8 qbytearray

Strt3 = qtextcodec: codecforname ("UTF-8")-> tounicode (s); // UTF-8 char * to Unicode qstring

Strt4 = qurl: topercentencoding (qstring: fromlocal8bit (s); // UTF-8 char * To percentencoding (UTF-8 format)

Strt5 = qurl: frompercentencoding ("0x % E6 % 88% 91% E4 % BB % ac"); // percentencoding (UTF-8 format) to Unicode

Bat4 = qtextcodec: codecforname ("gb2312")-> fromunicode (qtextcodec: codecforname ("UTF-8")-> tounicode (s); // UTF-8 char * To gb2312 qbytearray

A small function (for gb2312 URL percent conversion) is provided)

Void percentencoding2bytearray (qstring strinput, qbytearray & bytearrayout)

{

For (INT I = 0; I <strinput. Length ();)

{

If (0 = qstring: Compare (strinput. mid (I, 1), qstring ("% ")))

{

If (I + 2) <strinput. Length ())

{09 bytearrayout. append (strinput. mid (I +). toshort ));

I = I + 3; 11} 12 else13 {14 bytearrayout. append (strinput. mid (I, 1 ));

I ++;

}

}

Else

{

Bytearrayout. append (strinput. mid (I, 1 ));

I ++;

}

}

// For end

} // Percentencodin

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.