把unicode编码转成GB2312呢又怎么做?
解决方案 »
- T请教 :EECHART 画二维曲线,为什么不是按照我给定点的xvalue时候的赋值顺序画线呢?
- 鼠标形状如何改变
- 各位大虾请教WebBrowser FreeInstance 和 NewInstance 以及 InitInstance 具体用法。在线等待,急急!
- vclzip控件 可不可以设置为不压缩,只是打包!就像winrar的存储模式!!
- 大家都帮帮忙啦!如何实现分析.res和.dcr的资源结构!!!up有分哦!
- 请教:关于Shell+Plugin的问题!
- 请问如何实现自动关闭一个正在运行的应用程序?谢谢!
- 可能是关于SQL SERVER简单的问题吧
- webbrowser控件的“复制”功能为什么不起作用?
- 大家来找一找DELPHI IDE的BUG(5.0 AND 6.0)
- 编写ocx遇到的问题
- 谁能告诉我ListView控件如何控制任一行的文字颜色?
{*******************************************************}
{ }
{ Author: 王运龙 }
{ }
{ [email protected] }
{ }
{*******************************************************}
unit wnCCVMain;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
btnConvert: TButton;
rbtnInputHex: TRadioButton;
rbtnInputChara: TRadioButton;
memInput: TMemo;
panResult: TPanel;
rbtnUToA: TRadioButton;
rbtnAToU: TRadioButton;
Bevel1: TBevel;
gbxUnicode: TGroupBox;
memUnicode: TMemo;
gbxAscii: TGroupBox;
memAscii: TMemo;
procedure btnConvertClick(Sender: TObject);
procedure rbtnInputCharaClick(Sender: TObject);
procedure rbtnInputHexClick(Sender: TObject);
private
{ Private declarations } function GetHex(const aStr: string): string;
function GetChars(aHexStr: string): string;
//将UnicodeHex AscII字串转换为ANSI Ascii
function UnicodeHexToStr(const asUnicodeHex: string): string;
function ChinaToUnicode(const aWideStr: WideString): string;
function UnicodeHex(const aWideStr: WideString): string;
function FormatHexDisp(const asHex: string): string;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}function TForm1.UnicodeHexToStr(const asUnicodeHex: string): string;
var
i: Integer;
sTemp: string;
begin
//“中国网管程序:操作说明”的Unicode编码16进制为:
//4E2D 56FD 7F51 7BA1 7A0B 5E8F FF1A 64CD 4F5C 8BF4 660E
for i := 1 to Length(asUnicodeHex) do
begin
if i mod 4 = 0 then
begin
sTemp := Copy(asUnicodeHex, i - 3, 4);
sTemp := WideChar(StrToIntDef('$' + sTemp, 0));
Result := Result + sTemp;
end;
end;
end;
function TForm1.ChinaToUnicode(const aWideStr: WideString): string;
var
sUnicodeHex: string;
i : integer;
begin
for i := 1 to Length(aWideStr) do
begin
sUnicodeHex := Format('%.4x', [Word(aWideStr[i])]);
sUnicodeHex := Chr(StrToInt('$' + Copy(sUnicodeHex, 3, 2))) +
Chr(StrToInt('$' + copy(sUnicodeHex, 1, 2)));
Result := Result + sUnicodeHex;
end;
end;function TForm1.UnicodeHex(const aWideStr: WideString): string;
var
i: Integer;
begin
for i := 1 to length(aWideStr) do
begin
Result := Result + Format('%.4x', [Word(aWideStr[i])]);
end;
end;function TForm1.FormatHexDisp(const asHex: string): string;
var
i, iLen: Integer;
begin
Result := asHex;
iLen := Length(Result);
if Odd(iLen) then
begin
Result := '0' + Result;
Inc(iLen);
end; for i := iLen downto 1 do
begin
if Odd(i) then Continue;
Insert(' ', Result, i - 1);
end;
Result := Trim(Result);
end;procedure TForm1.btnConvertClick(Sender: TObject);
var
s: string;
begin
s := memInput.Text;
if rbtnInputChara.Checked then
begin
memUnicode.Text := FormatHexDisp(UnicodeHex(s));
memAscii.Text := FormatHexDisp(GetHex(s));
end else
begin
s := StringReplace(s, ' ', '', [rfReplaceAll, rfIgnoreCase]);
if rbtnUToA.Checked then
begin
memUnicode.Text := UnicodeHexToStr(s);
memAscii.Text := FormatHexDisp(GetHex(memUnicode.Text));
end else
begin
memUnicode.Text := GetChars(s);
memAscii.Text := UnicodeHex(memUnicode.Text);
end;
end;
end;procedure TForm1.rbtnInputCharaClick(Sender: TObject);
begin
memUnicode.Clear;
memAscii.Clear;
gbxUnicode.Caption := 'Unicode编码';
gbxAscii.Caption := 'Ascii编码';
rbtnUToA.Enabled := False;
rbtnAToU.Enabled := False;
end;procedure TForm1.rbtnInputHexClick(Sender: TObject);
begin
memUnicode.Clear;
memAscii.Clear; gbxUnicode.Caption := '转换后的文本';
gbxAscii.Caption := 'Hex编码';
rbtnUToA.Enabled := True;
rbtnAToU.Enabled := True;
end;function TForm1.GetHex(const aStr: string): string;
var
i: Integer;
begin
for i := 1 to Length(aStr) do
begin
Result := Result + Format('%.2x', [Ord(aStr[i])]);
end;
end;function TForm1.GetChars(aHexStr: string): string;
var
i: Integer;
begin
aHexStr := StringReplace(aHexStr, ' ', '', [rfReplaceAll, rfIgnoreCase]);
for i := 1 to Length(aHexStr) do
begin
if Odd(i) then
begin
Result := Result + Char(StrToIntDef('$' + Copy(aHexStr, i, 2), 0));
end;
end;
end;end.
// 中文格式编码,s为Unicode String
function Encode2(var s:WideString):String;
var
i,len:Integer;
cur:Integer;
t:String;
begin
Result:=‘’;
len:=Length(s);
i:=1;
while i<=len do
begin
cur:=ord(s[i]);
//BCD转换
FmtStr(t,‘%4.4X’,[cur]);
Result:=Result+t;
inc(i);
end;
end;
var ws: array[0..32767] of WideChar;
s: array[0..65535] of char;
begin
ZeroMemory(@ws, Length(ws) * SizeOf(WideChar));
ZeroMemory(@s, Length(s) * SizeOf(char));
MultiByteToWideChar(950, MB_COMPOSITE, PChar(sBig5), -1, @ws, Length(ws));
WideCharToMultiByte(936, WC_COMPOSITECHECK, @ws, -1, @s, Length(s), '?', PBOOL(false));
result := s;
end;
若要達到轉換後轉繁簡寫法不同的話,以上程式不適用
4. WideCharToMultiByte,第一個參數傳入 936有關 code page 請看 Windows SDK Help 中 IsValidCodePage 的說明简体操作系统下,
对GB2312,把 MultiByteToWideChar 的第一個參數傳入 936,得到的不是UNICODE,而是GBK的简体
同样,对Big5,传入950,得到的也是GBK的繁体,不是UNICODE,
只有把结果再用UTF8Encode函数,转一下,才得到UNICODE(UTF8),
你把结果放到一个htm文件,在IE里试试就知道了,套用不同的编码,看到不一样的乱码在简体系统,转成GBK的繁体,CharSet用Default就能显示“繁体”了,但由于这个“繁体”是BGK的“繁体”,拿到繁体操作系统(Big5)下,一样会是乱码