Correct character encoding
There are three common Chinese encoding formats: 1) gb2312 (namely, ASCII Code, also known as ANSI) 2) Unicode 3) UTF-8
C ++ generally uses the ancii code, while Java generally uses Unicode, while symbianc ++ (later referred to as Symbian) uses Unicode or UTF-8
For example, the Chinese character "good": Unicode is "7d 59", ASCII is "ba C3", and UTF-8 is encoded into three bytes.
Of course, you can choose to use char * and unsigned char * on the terminal, but this is not recommended. In many cases, such as the interface, you need to convert it to the character encoding format of the terminal.
Now we will explain how to convert characters in four parts (assume that UTF-8 is used between Java and Symbian, and ASCII code is used between C ++ and Symbian ):
1) Symbian-> JAVA sends messages containing Chinese Characters
Assume that your Symbian message sending interface uses hbufc8 * (UTF-8)
Before sending your message, you need to convert the data into UTF-8 format,
For example, you can use cnvutfconverter: convertfromunicodetoutf8 () to convert the tbuf of the interface to an 8-bit UTF-8 format. During the conversion process, pay special attention to the following, the memory opened for hbufc8 * should be twice the tbuf length!
As for how the terminal processes the messages sent by the Java end, Java usually sends only one string, that is, a unicode string. At this time, Symbian generally receives the messages by tdes8, in fact, the received data is Unicode rather than UTF-8, But Symbian supports this format, so it can be processed directly in this way without conversion. Of course, cnvutfconverter: converttounicodefromutf8 () is required for display on the interface ())
2) Java-> Symbian sends messages containing Chinese Characters
Of course, the server must also process Chinese characters. As we have already said, You can directly send string (UNICODE) during sending so that Symbian can parse it, but be very careful when receiving it !! The terminal Symbian comes in the hbufc8 * (UTF-8) format. Java should not directly use string to receive the data. It needs to receive the data in bytes, parse the data into UTF-8, and convert it to string, there are several simple statements to complete this operation:
Byte B3 [] =...; // UTF-8 bytes from Symbian
Datainputstream Dis = new datainputstream (New bytearrayinputstream (B3 ));
String chinastring = dis. readutf ();
3) send a message containing Chinese Characters in Symbian-> C ++
Now let's discuss how to send the Symbian message to the C ++ subsystem. Obviously, the C ++ subsystem processes the string according to char */unsigned char, that is, the ASCII code (Chinese is the extended ANSI, that is, gb2312 ).
Although I didn't use Symbian to send Chinese characters to C ++ in my project, the following two functions make it easier.
Tptrc8 aptrstring (isomebuf)
Char * acharstring = (char *) aptrstring. ptrz ()
After receiving such a message, the C ++ system needs to build it without any conversion.
4) C ++-> Symbian sends messages containing Chinese Characters
C ++ transmission is still in char * ASCII format.
It's a little troublesome for the terminal to receive it !! In this way, the terminal should be especially careful. For example, if the message sending has a tuint8 message header, you need to remove it first, and then convert the rest to 16-bit Unicode, so that the interface can be displayed, if you want to perform 8-bit processing, use the cnvutfconverter: convertfromunicodetoutf8 () function.
Void convunicodel (const tdesc8 & atext, tdes16 & unicodetext, tuint8 codetype)
{
Ccnvcharactersetconverter * aconverter = ccnvcharactersetconverter: newlc ();
Tuint code;
Code = kcharactersetidentifiergbk;
If (aconverter-> preparetoconverttoorfroml (code,
Ceikonenv: static ()-> fssession ())! = Ccnvcharactersetconverter: eavailable)
User: Leave (kerrnotsupported );
Tint state = ccnvcharactersetconverter: kstatedefault;
Const tint returnvalue = aconverter-> converttounicode (unicodetext, atext, State );
If (returnvalue = ccnvcharactersetconverter: eerrorillformedinput)
User: Leave (kerrcorrupt );
Else if (returnvalue <0)
User: Leave (kerrgeneral );
Cleanupstack: popanddestroy ();
Tint loc = unicodetext. Locate (0); // remove the additional 0
If (loc! = Kerrnotfound)
{
Unicodetext. Delete (LOC, unicodetext. Length ()-loc );
}
// Add the line of code cnvutfconverter: convertfromunicodetoutf8 () Here if UTF-8 is to be converted ()
}
Is it difficult to recall it now? Correct. The above processing method is indeed very troublesome for the terminal.
Imagine some rules:
1) all the messages in the system are transmitted using UTF-8, and Symbian may laugh. Thank you, C ++ may be confused, and Java may be slightly converted.
2) messages in all systems use ASCII codes, and C ++ may laugh. Haha, I have been using char *, and Symbian is really more troublesome to handle. Java is slightly converted.
3) If Unicode is used for messages in all systems, Java may laugh. You should have done this before, and Symbian will be happy to know how C ++ converts messages.