Delphi Operation Unicode Characters

Source: Internet
Author: User
Tags integer ord

===================================delphi:===================================================
The default state in the current version (2007), String is ansistring
In Delphi 2009:
string = unicodestring; (same: pstring = punicodestring;)
Char = Widechar; (same: PChar = Pwidechar;)

Procedure
Var
List:tstrings;
Begin
List: = tstringlist.create;
List.text: = str;
List.savetofile (FilePath, TENCODING.ASCII);
List.loadfromfile (FilePath, TENCODING.ASCII); Tencoding.utf8;
Memo2.lines: = List;
List.free;
End

Ask the next million teacher: I found delphi2009 with idhttp to get a webpage, if the source of the Web page is UTF8 can be directly used Htmsrc:=idhttp.get (URL), and if it is GB code can only use stream mode. But how does the GB-coded page obtained with stream turn him into UTF? If not, the non-Chinese operating system will be garbled.
Procedure Tform1.button1click (Sender:tobject);
Var
Stream1,stream2:tstringstream;
B:byte;
bs:string;
Begin
{Create the first stream, use the default double-byte encoding; the data in the stream is a string in Memo}
STREAM1: = Tstringstream.create (Memo1.text, 54936);
{The hexadecimal encoding of the first stream is displayed in Memo}
BS: = ';
For b in stream1. Bytes do bs: = Format (bs + '%2x ', [b]);
MEMO1.LINES.ADD (BS);
STREAM2: = Tstringstream.create (stream1. Datastring, Tencoding.utf8);
{The hexadecimal encoding of the second stream is displayed in Memo}
BS: = ';
For b in Stream2. Bytes do bs: = Format (bs + '%2x ', [b]);
MEMO1.LINES.ADD (BS);
Stream1. Free;
Stream2. Free;

function Str_gb2unicode (text:string): string;
Var
I,len:integer;
Cur:integer;
t:string;
ws:widestring;
Begin
Result: = ';
WS: = text;
Len: = Length (WS);
I: = 1;
While I <= Len do
Begin
Cur: = Ord (Ws[i]);
Fmtstr (t, '%4.4x ', [cur]);
Result: = result + t;
INC (i);
End
End


function Unicode_str (text:string): string;
Var
I,len:integer;
ws:widestring;
Begin
WS: = ";
I: = 1;
Len: = Length (text);
While I < Len do
Begin
WS: = ws + Widechar (strtoint (' $ ' + Copy (text,i,4)));
I: = i+4;
End
Result: = WS;
End


Procedure Tform1.button1click (Sender:tobject);
Begin
ShowMessage (Str_gb2unicode (' íòò» ')); 4e074e00
ShowMessage (Unicode_str (' 4e074e00 ')); Íòò»
End


Functions for converting Unicode and characters to each other
2009-11-09 14:52
Convert characters to Unicode
function Ansitounicode (ansi:string): string;
Var
s:string;
I:integer;
J,K:STRING[2];
A:array [1..1000] of char;
Begin
S:= ";
Stringtowidechar (ansi,@ (a[1]), 500);
I:=1;
while ((A[i]<> #0) or (a[i+1]<> #0)) DO begin
J:=inttohex (Integer (A[i]), 2);
K:=inttohex (Integer (a[i+1]), 2);
S:=s+k+j;
i:=i+2;
End
Result:=s;
End

Convert Unicode to Characters
function Readhex (astring:string): integer;
Begin
Result:=strtoint (' $ ' +astring)
End

function Unicodetoansi (unicode:string): string;
Var
s:string;
I:integer;
J,K:STRING[2];
Begin
I:=1;
S:= ";
While i< if end; S:= "S+char (Readhex (j)) +char (Readhex (k))," i:= "i+4;" k:= "Copy (unicode,i,2);" j:= "Copy (unicode,i+2,2);" Begin Do> " ' Then
S:=widechartostring (Pwidechar (s+ #0 #0#0#0))
Else
S:= ";
Result:=s;
End


Widechar compatible with the Ansichar #0: #255; But it takes 2 bytes.
UniCode character Widechar; Unlike Ansichar, Widechar is 2 bytes in size.
Var
C:widechar; {The value range for Widechar is: #0 #65535, in hexadecimal notation: #$0..# $FFFF}
Begin
{Widechar is compatible with Ansichar #0: #255; but it takes up 2 byte size}
c: = #65;
ShowMessage (c); A
ShowMessage (IntToStr (Length (c))); {1; This is the character length}
ShowMessage (IntToStr (SizeOf (c))); {2; but takes 2 bytes}

Navigation: Questions and Answers >

The conversion of Chinese characters and multi-byte encoding


Conversion of Chinese characters to multibyte encoding-reply to "Don't know" problem

Source of the problem:

Tencoding.default code (in the 16-bit CE D2 C3 C7) How to turn into Chinese characters?

Chinese characters are ' we ';

--------------------------------------------------------------------------------
Delphi 2009 The default encoding is multibyte encoding (MBCS), which Delphi represents it: Tencoding.default.

Here is an example of the conversion between multibyte encoding and kanji:

--------------------------------------------------------------------------------


{Kanji to multibyte encoding}

Procedure Tform1.button1click (Sender:tobject);

Var

Stream:tstringstream;

B:byte;

String

Begin

Stream: = Tstringstream.create (' we ', tencoding.default);
s: = ';
for b in stream. Bytes do s: = Format ('%s%x '
ShowMessage (s); {CE D2 C3 C7}
Stream. Free;

End

{Multi-byte encoding to kanji}

Procedure Tform1.button2click (Sender:tobject);
Var
Stream:tstringstream;
Begin

Stream: = Tstringstream.create;
Stream. Size: = 4;
Stream. Bytes[0]: = $CE;
Stream. BYTES[1]: = $D 2;
Stream. BYTES[2]: = $C 3;
Stream. BYTES[3]: = $C 7;
ShowMessage (stream. datastring);
Stream. Free;

End

{Convert multibyte-encoded strings to Kanji}

Procedure Tform1.button3click (Sender:tobject);
Var
Stream:tstringstream;
I:integer;
Begin
str: = ' ced2c3c7 ';
Stream: = Tstringstream.create;
Stream. Size: = Length (str) Div 2;
For I: = 1to Length (str) does
If ODD (i) then stream. Bytes[i Div 2]: = Strtointdef (Concat (#36, str[i],str[i+1]), 0);
ShowMessage (stream. datastring); We
Stream. Free;
End
End.


Get a table of all Chinese characters and Unicode
Var
w:widestring;
I:integer;
s:string;
List:tstringlist;
Begin
List: = tstringlist.create;
For I: = $4e00 to $9fa5 do
Begin
S: = #36 + Inttohex (i,4); {#36 is a $ character}
W: = Widechar (i);
List.add (s + ' = ')
End

List.savetofile (' C:\temp\Unicode-Hz.txt ');
List.free;
End

Kanji and Unicode Conversions
{Thank you Robin (xuebin418@163.com) for providing}

Transformation

functionstring): String;
Var
I,len:integer;
Cur:integer;
t:string;
ws:widestring;
Begin
Result: = ';
WS: = text;
Len: = Length (WS);
I: = 1
While I <= Len do
Begin
Cur: = Ord (Ws[i]);
Fmtstr (t, '%4.4x ', [cur]);
Result: = result + t;
INC (i);
End

End

Recovery
Unicode_str (text:string): string;
Var
I,len:integer;
ws:widestring;
Begin
WS: = ";
I: = 1;
Len: = Length (text);
While I < Len do
Begin
WS: = ws + Widechar (strtoint (' $ ' + Copy (text,i,4)));
I: = i+4;
End
Result: = WS;
End

Test
Procedure Tform1.button1click (Sender:tobject);
Begin
ShowMessage (Str_gb2unicode (' in case ')); 4e074e00
ShowMessage (Unicode_str (' 4e074e00 ')); If
End

The strings in the Windows API correspond to this Delphi PChar (Pansichar); The use of Delphi strings in the API is still more flexible.
Fixed-length strings are not #0 end, and the API is not compatible with, generally not used in the API.
Assignment Method 1: Give direct volume

Begin
SetWindowText (Handle, ' new title ');
End

--------------------------------------------------------------------------------

Assignment Method 2: Define the type it wants
Var
P:pchar;
Begin
P: = ' new title ';
SetWindowText (Handle, p);
End

--------------------------------------------------------------------------------

Assignment Method 3: Convert to the type it wants
Var
str:string;
Begin
str: = ' new title ';
SetWindowText (Handle, PChar (str));
End

Assignment Method 4: Using a character array
Var
ARR:ARRAY[0..255] of Char;
Begin
Arr: = ' new title ';
SetWindowText (Handle, arr);
End

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.