Home > Back-end >  Delphi how to convert Chinese characters into GB2312, such as "wuhan" to wuhan
Delphi how to convert Chinese characters into GB2312, such as "wuhan" to wuhan

Time:09-17

Delphi how to convert Chinese characters into GB2312, such as "wuhan" into CE BA BA E4 % % % % or into CEE4BABA, it's better if can turn a word, supports and punctuation in both Chinese and English mixed conversion

CodePudding user response:

Type
GB2312String=type AnsiString (936);
Var
S: a string;
S1: GB2312String;
I: integer;
The begin
S:='wuhan';
S1:=S;
For I:=0 to Length - 1 (S1) do
Write (IntToHex (PByte [I] (S1), 2));
end;

CodePudding user response:

If the version for XE, it can be
The Function StrToGB2312 (sStr: String) : String;
Var
I, lLen: integer;
LAStr: AnsiString;
The Begin
LAStr:=AnsiString (sStr);//xe defaults to widestring
LLen:=Length (lAStr);
Result:=';
For I:=1 To lLen Do
Result: the Result=+ IntToHex (word (lAStr [I]), 2);
End

CodePudding user response:

936/1386/54936 AnsiString must specify the code page, or in default of simplified Chinese character system will run on conversion errors,

CodePudding user response:

 
Type
Tz=record
Case Byte of
0: (str1: array [0.. 1] of WideChar);
1: : [str2 string [4]);
2: (strcode1: word;
Strcode2: word);
end;
Procedure TForm2. Button11Click (Sender: TObject);
Var
A: tz.
The begin
A.s tr2:='wuhan';
ShowMessage (format (' % s, % s', [IntToHex (a.s trcode1, 2), IntToHex (a.s trcode2, 2)]));
//CE04 BAE4
A.s tr1 [0] :=widestring (' arms') [1];
A.s tr1 [1] :=widestring (' han ') [1];
ShowMessage (format (' % s, % s', [IntToHex (a.s trcode1, 2), IntToHex (a.s trcode2, 2)]));
//6 b66, c49
end;

Is that ok? Only two out of the results are different,

CodePudding user response:

//URL address translation 
The function UrlEncode (Url: string) : string;
Var
I: Integer;
Raws: RawByteString;
The begin
Result:=';
Raws:=Utf8Encode (Url);
For I:=1 to Length (raws) do
Case raws [I] of
'! ', '#', '$',' & amp; ', ' ' ' ', '(',') ', '*', '+', ', ', '-', '. ', '/', ':'
'; 'and'=', '? ', '@', '_', '-', '0'.. '9', 'a'.. 'z', 'A'.. 'Z' :
Result: the Result=+ raws [I];
The else
Result: the Result +='%' + IntToHex (word (raws [I]), 2);
end;
end;

CodePudding user response:

 
USES IdURI;

TIdURI. ParamsEncode (' wuhan)

CodePudding user response:

Try URLEncode (' wuhan)
  • Related