Conversion of Chinese characters and Unicode codes

Source: Internet
Author: User

During the summer vacation, I didn't go home. I stayed at school to do a text message sending project. The conversion of Chinese characters and Unicode codes made me quite awkward. At the beginning, I started to use C language, and there was always garbled code, it took a long time to get stuck. Later, I accidentally saw it on the Internet and said it could be implemented using QT ,.. Haha, I found a piece of code on the Internet and finally solved it... To share:

# Ifndef widget_h
2 # define widget_h
3 # include <qwidget>
4 class Widget: Public qwidget
5 {
6 q_object
7
8 public:
9 widgets (qwidget * parent = 0 );
10 qstring stringtounicode (qstring Str );
11 qstring unicodetostring (qstring Str );
12
13
14 };
15
16
17 # endif

# Include <qtgui/qapplication>
2 # include <qstring>
3 # include <qobject>
4 # include <qdebug>
5 # include <qtextcodec>
6 # include "Main. H"
7
8
9 Widget: widget (qwidget * parent): qwidget (parent)
10 {
11}
12
13 qstring Widget: stringtounicode (qstring Str)
14 {
15
16
17 // tr must be added to the string sent here, And qtextcodec: setcodecfortr (qtextcodec: codecforlocale () can be added to the main function ());
18
19 // example: Str = tr ("hello ");
20
21 const qchar * q;
22
23 qchar qtmp;
24
25 qstring str0, Strout;
26
27 int num;
28
29 q = Str. Unicode ();
30
31 int Len = Str. Count ();
32
33 for (INT I = 0; I <Len; I ++)
34
35 {qtmp = (qchar) * q ++;
36
37 num = qtmp. Unicode ();
38
39 if (Num & lt; 255)
40
41 Strout + = "00"; // Add "00" before an English letter or number"
42
43
44
45 str0 = str0.setnum (Num, 16); // convert to hexadecimal number
46
47
48
49 Strout + = str0;
50
51}
52
53
54
55
56
57 Return Strout;
58
59
60
61}
62
63 qstring Widget: unicodetostring (qstring Str)
64
65 {
66
67 // For example, STR = "4f60597d ";
68
69 int temp [400];
70
71. qchar [100];
72
73 qstring Strout;
74
75 bool OK;
76
77 int COUNT = Str. Count ();
78
79 int Len = count/4;
80
81 For (INT I = 0; I <count; I + = 4)
82

83 {
84
85 temp [I] = Str. mid (I, 4). toint (& OK, 16); // convert every four digits to a hexadecimal integer.
86
87 qchar [I/4] = temp [I];
88
89 qstring str0 (qchar, Len );
90
91 Strout = str0;
92
93}
94
95 return Strout;
96
97}
98
99 int main (INT argc, char * argv [])
100 {
101
102 qapplication app (argc, argv );
103
104 widget * WID = new widget;
105
106 qtextcodec: setcodecfortr (qtextcodec: codecforlocale ());
107 qstring str_c = qobject: TR ("have a good time! ");
108
109 // qstring STR = "5de54f5c61095febff01"; // Unicode code that works well
110 qstring STR = "5de54f5c61095feb00200021"; // It is also a good Unicode code
111
112 qstring str1 = wid-> unicodetostring (STR );
113 qstring str2 = wid-> stringtounicode (str_c );
114
115 qdebug () <str1;
116 qdebug () <str2;
117
118 return app.exe C ();
119
120
121}

Finally, I can go home for a good summer vacation and meet my big 3 !!! 79,1-8 50%

~

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.