Delphi encoding conversion

Source: Internet
Author: User

The following is the source code:

1. convert any multi-byte language to UNICODE:
Function unicodeencode (STR: string; codePage: integer): widestring;
VaR
Len: integer;
Begin
Len: = length (STR) + 1;
Setlength (result, Len );
Len: = multibytetowidechar (codePage, 0, pchar (STR),-1, pwidechar (result), Len );
Setlength (result, len-1); // end is #0
End;

Function unicodedecode (STR: widestring; codePage: integer): string;
VaR
Len: integer;
Begin
Len: = length (STR) * 2 + 1; // One for #0
Setlength (result, Len );
Len: = widechartomultibyte (codePage, 0, pwidechar (STR),-1, pchar (result), Len, nil, nil );
Setlength (result, len-1 );
End;

For example, if the code page of gb2312 is 936, you can convert gb2312 to Unicode as follows:
Unistr: = unicodeencode (gbstr, 936 );
You can convert the Unicode Internal code to gb2312 as follows:
Gbstr: = unicodedecode (unistr, 936 );
The same is true for other languages, as long as you know the code page.
Note: The Japanese code page is 932.

2. file encoding Transformation
First, let's take a look at the following tips:

Ansitoutf8
Utf8tounicode
Let's take a look at the help of these two functions.

You can use a tstringlist to load the text file, declare a widestring variable, and finally call stringtowidechar. The converted information is stored in widestring.
Declare another memorystream and write the widestring into the stream.
Finally, save the stream as a text file.

You can see it in a file. It is unicode encoded and has the same information.

The following is the ultimate version:
Each text format has an identifier at the beginning to identify the encoding. For example, the start of Unicode is fffe. The notebook identifies the code based on the marker at the beginning.

When you write the above, you didn't write the logo at the beginning of the file, so you couldn't identify it when you open it in your notebook. If you don't believe it, you can choose to save it, it can be seen that it is recognized as an ANSI code, so it will show incorrect.

The correct method is to write the fffe mark at the beginning of the file so that it can be correctly displayed.

The following is the source code:
VaR SL: tstringlist;
MM: tmemorystream;
Anstr: string;
WS: widestring;
P: pbytearray;
Begin
SL: = tstringlist. Create;
MM: = tmemorystream. Create;
Try
Sl.loadfromfile('test.txt ');
Anstr: = SL. text;
WS: = anstr; // convert to Unicode

// Unicode mark written at the beginning of the text
Getmem (p, 2 * sizeof (char ));
P [0]: = $ ff;
P [1]: = $ Fe;
Mm. Write (P ^, 2 * sizeof (char ));

// Write the Unicode text into the stream and save it.
Mm. Write (pwidechar (WS) ^, length (WS) * sizeof (widechar ));
Mm.savetofile('test2.txt ');

Freemem (P );
Finally
SL. Free;
Mm. Free;
End;
End;

I wrote "yes, this is Unicode" in a notebook and saved it as a test file. In this case, it is ANSI encoded.
After the code above is executed, the text is saved as Test2, which is the Unicode code and can be correctly displayed. You can save it as a unicode code to see if it is Unicode encoding.

Then another buddy said:

Warmworm (warmworm)

Thick
I used to convert ANSI to Unicode. I read these functions, but they are actually the simplest method.

Yes
ANSI-> Unicode
Strunicode: = widestring (stransi );

Unicode-> ANSI
Stransi: = ansistring (strunicode );

In another post:

Aiirii (Ari-http://spaces.msn.com/members/aiirii)

Unicode text file: The first two characters are FF Fe (hexadecimal)
Unicode big endian text file: The first two characters are Fe ff (hexadecimal, big endian will naturally be the opposite)

UTF-8 text file: The first two characters are ef bb (hexadecimal) (Supplement: To identify the UTF-8 encoded file by writing the file header, write (logfile, # $ EF # $ BB # $ BF );)

You can use NotePad to open a text file, save it as a file in various ways, and then use a hexadecimal editor to see it.


 

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.