百万火急,求简繁转换的实现,做了好久都没有成功!(10分)

  • 主题发起人 主题发起人 zpselect
  • 开始时间 开始时间
Z

zpselect

Unregistered / Unconfirmed
GUEST, unregistred user!
如题,我在网上找了一些简体转繁体的代码,可是在繁体操作系统下运行也是乱码,请问这是怎么回事?有的人说,简体系统的字体与繁体系统下的字体不同,所以转换过来是乱码!
我把代码贴出来,望各位指点!
function TForm1.GB2Big5(GBStr : String): AnsiString;
{进行GB转BIG5内码}
var
Len: Integer;
pGBCHTChar: PChar;
pGBCHSChar: PChar;
pUniCodeChar: PWideChar;
pBIG5Char: PChar;
begin
pGBCHSChar := PChar(GBStr);
Len := MultiByteToWideChar(936,0,pGBCHSChar,-1,nil,0);
GetMem(pGBCHTChar,Len*2+1);
ZeroMemory(pGBCHTChar,Len*2+1);
//GB CHS -> GB CHT
LCMapString($804,LCMAP_TRADITIONAL_CHINESE,pGBCHSChar,-1,pGBCHTChar,Len*2);
GetMem(pUniCodeChar,Len*2);
ZeroMemory(pUniCodeChar,Len*2);
//GB CHT -> UniCode
MultiByteToWideChar(936,0,pGBCHTChar,-1,pUniCodeChar,Len*2);
Len := WideCharToMultiByte(950,0,pUniCodeChar,-1,nil,0,nil,nil);
GetMem(pBIG5Char,Len);
ZeroMemory(pBIG5Char,Len);
//UniCode -> Big5
WideCharToMultiByte(950,0,pUniCodeChar,-1,pBIG5Char,Len,nil,nil);
Result := String(pBIG5Char);
FreeMem(pBIG5Char);
FreeMem(pGBCHTChar);
FreeMem(pUniCodeChar);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Label1.Caption:=GB2Big5(Label1.Caption);
end;
注意:Label1.Caption为:中华人民共和国
 
修改如下:
procedure TForm1.FormCreate(Sender: TObject);
var
tmpCap: string;
Len: Integer;
begin
Len := Label1.GetTextLen;
SetString(tmpCap, PChar(nil), Len);
Label1.GetTextBuf(Pointer(tmpCap), Len + 1);
Label1.SetTextBuf(PChar(GB2Big5(tmpCap)));
end;
 
加了那几句显示还是乱码!
 
nicai_wgl说的有道理,但是最好全部是PCHAR操作,用string还是会有问题,给你一点代码,看是否有帮助
function GBCht2Chs(GBStr : pchar): AnsiString;overload;
{进行GBK繁体转简体}
function GBChs2Cht(GBStr : pchar): AnsiString;overload;
{进行GBK简体转繁体}
前面几个函数还是可能会出错,但是上面这两个函数采用PCHAR,就没有问题了,你可以参考来改前面几个函数
unit Codechange;
interface
uses
windows;

function Big52GB(BIG5Str : String): AnsiString;
{进行big5转GB内码}
function GB2Big5(GBStr : String): AnsiString;
{进行GB转BIG5内码}
function GBCht2Chs(GBStr : String): AnsiString;overload;
{进行GBK繁体转简体}
function GBChs2Cht(GBStr : String): AnsiString;overload;
function GBCht2Chs(GBStr : pchar): AnsiString;overload;
{进行GBK繁体转简体}
function GBChs2Cht(GBStr : pchar): AnsiString;overload;
{进行GBK简体转繁体}
implementation

function Big52GB(BIG5Str : String): AnsiString;
{进行big5转GB内码}
var
Len: Integer;
pBIG5Char: PChar;
pGBCHSChar: PChar;
pGBCHTChar: PChar;
pUniCodeChar: PWideChar;
begin
//String -> PChar
pBIG5Char := PChar(BIG5Str);
Len := MultiByteToWideChar(950,0,pBIG5Char,-1,nil,0);
GetMem(pUniCodeChar,Len*2);
ZeroMemory(pUniCodeChar,Len*2);
//Big5 -> UniCode
MultiByteToWideChar(950,0,pBIG5Char,-1,pUniCodeChar,Len);
Len := WideCharToMultiByte(936,0,pUniCodeChar,-1,nil,0,nil,nil);
GetMem(pGBCHTChar,Len*2);
GetMem(pGBCHSChar,Len*2);
ZeroMemory(pGBCHTChar,Len*2);
ZeroMemory(pGBCHSChar,Len*2);
//UniCode->GB CHT
WideCharToMultiByte(936,0,pUniCodeChar,-1,pGBCHTChar,Len,nil,nil);
//GB CHT -> GB CHS
LCMapString($804,LCMAP_SIMPLIFIED_CHINESE,pGBCHTChar,-1,pGBCHSChar,Len);
Result := String(pGBCHSChar);
FreeMem(pGBCHTChar);
FreeMem(pGBCHSChar);
FreeMem(pUniCodeChar);
end;

function GB2Big5(GBStr : String): AnsiString;
{进行GB转BIG5内码}
var
Len: Integer;
pGBCHTChar: PChar;
pGBCHSChar: PChar;
pUniCodeChar: PWideChar;
pBIG5Char: PChar;
begin
pGBCHSChar := PChar(GBStr);
Len := MultiByteToWideChar(936,0,pGBCHSChar,-1,nil,0);
GetMem(pGBCHTChar,Len*2+1);
ZeroMemory(pGBCHTChar,Len*2+1);
//GB CHS -> GB CHT
LCMapString($804,LCMAP_TRADITIONAL_CHINESE,pGBCHSChar,-1,pGBCHTChar,Len*2);
GetMem(pUniCodeChar,Len*2);
ZeroMemory(pUniCodeChar,Len*2);
//GB CHT -> UniCode
MultiByteToWideChar(936,0,pGBCHTChar,-1,pUniCodeChar,Len*2);
Len := WideCharToMultiByte(950,0,pUniCodeChar,-1,nil,0,nil,nil);
GetMem(pBIG5Char,Len);
ZeroMemory(pBIG5Char,Len);
//UniCode -> Big5
WideCharToMultiByte(950,0,pUniCodeChar,-1,pBIG5Char,Len,nil,nil);
Result := String(pBIG5Char);
FreeMem(pBIG5Char);
FreeMem(pGBCHTChar);
FreeMem(pUniCodeChar);
end;

function GBCht2Chs(GBStr : String): AnsiString;overload;
{进行GBK繁体转简体}
var
Len: Integer;
pGBCHTChar: PChar;
pGBCHSChar: PChar;
begin
pGBCHTChar := PChar(GBStr);
Len := MultiByteToWideChar(936,0,pGBCHTChar,-1,nil,0);
GetMem(pGBCHSChar,Len*2+1);
ZeroMemory(pGBCHSChar,Len*2+1);
//GB CHS -> GB CHT
LCMapString($804,LCMAP_SIMPLIFIED_CHINESE,pGBCHTChar,-1,pGBCHSChar,Len*2);
Result := String(pGBChsChar);
//FreeMem(pGBCHTChar);
FreeMem(pGBCHSChar);
end;

function GBCht2Chs(GBStr : PCHAR): AnsiString;overload;
{进行GBK繁体转简体}
var
Len: Integer;
pGBCHTChar: PChar;
pGBCHSChar: PChar;
begin
pGBCHTChar := GBStr;
Len := MultiByteToWideChar(936,0,pGBCHTChar,-1,nil,0);
GetMem(pGBCHSChar,Len*2+1);
ZeroMemory(pGBCHSChar,Len*2+1);
//GB CHS -> GB CHT
LCMapString($804,LCMAP_SIMPLIFIED_CHINESE,pGBCHTChar,-1,pGBCHSChar,Len*2);
Result := String(pGBChsChar);
//FreeMem(pGBCHTChar);
FreeMem(pGBCHSChar);
end;

function GBChs2Cht(GBStr : String): AnsiString;overload;
{进行GBK简体转繁体}
var
Len: Integer;
pGBCHTChar: PChar;
pGBCHSChar: PChar;
begin
pGBCHSChar := PChar(GBStr);
Len := MultiByteToWideChar(936,0,pGBCHSChar,-1,nil,0);
GetMem(pGBCHTChar,Len*2+1);
ZeroMemory(pGBCHTChar,Len*2+1);
//GB CHS -> GB CHT
LCMapString($804,LCMAP_TRADITIONAL_CHINESE,pGBCHSChar,-1,pGBCHTChar,Len*2);
Result := String(pGBCHTChar);
FreeMem(pGBCHTChar);
//FreeMem(pGBCHSChar);
end;

function GBChs2Cht(GBStr : pchar): AnsiString;overload;
{进行GBK简体转繁体}
var
Len: Integer;
pGBCHTChar: PChar;
pGBCHSChar: PChar;
begin
pGBCHSChar := GBStr;
Len := MultiByteToWideChar(936,0,pGBCHSChar,-1,nil,0);
GetMem(pGBCHTChar,Len*2+1);
ZeroMemory(pGBCHTChar,Len*2+1);
//GB CHS -> GB CHT
LCMapString($804,LCMAP_TRADITIONAL_CHINESE,pGBCHSChar,-1,pGBCHTChar,Len*2);
Result := String(pGBCHTChar);
FreeMem(pGBCHTChar);
//FreeMem(pGBCHSChar);
end;
 
procedure TForm1.FormCreate(Sender: TObject);
var
tmpCap: string;
Len: Integer;
begin
Len := Label1.GetTextLen;
SetString(tmpCap, PChar(nil), Len);
Label1.GetTextBuf(Pointer(tmpCap), Len + 1);
showMessage(tmpCap);
Label1.SetTextBuf(PChar(GBToBig5(tmpCap)));
end;
我是这么写的啊!但是在繁体系统下面还是乱码的!
是否有成功的简体转成繁体的程序代码啊?
 
我以前也为这个东东烦恼,后来采用分别做个简体ini文件和繁体ini文件,解决了这个问题
 
ini文件,是利用字码对照的方法么?
你原先用过API函数做过吗?
 
是用字体对照方法 以前做过
 
我也做过用字体对照的方式,但是在繁体系统下运行还是乱码!我听一个同事说:简体系统的字体与繁体系统下的字体不一样,需要在繁体系统下改变成字体才行!但我也试了,也不行的,代码如下:
procedure TForm1.FormCreate(Sender: TObject);
begin
if SysLocale.DefaultLCID = 2052 then
begin
Font.Name := '宋体';
Font.Size := 9;
Font.Charset := GB2312_CHARSET;
end
else
if SysLocale.DefaultLCID = 1028 then
begin
Font.Name := '新細明體';
Font.Size := 9;
Font.Charset := CHINESEBIG5_CHARSET;
end
else
begin
Font.Name := 'Tahoma';
Font.Size := 9;
Font.Charset := ANSI_CHARSET;
end;
Label1.Caption := GBToBig5(Label1.Caption);
Edit1.Text := GBTOBIG5(Edit1.Text);
end;
 
这是我网络上找的,自己的测试程序:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
function GBChtToChs(GBStr: string): AnsiString;
{进行GBK繁体转简体}
var
Len: Integer;
pGBCHTChar: PChar;
pGBCHSChar: PChar;
begin
pGBCHTChar := PChar(GBStr);
Len := MultiByteToWideChar(936, 0, pGBCHTChar, -1, nil, 0);
GetMem(pGBCHSChar, Len * 2 + 1);
ZeroMemory(pGBCHSChar, Len * 2 + 1);
//GB CHS -> GB CHT
LCMapString($804, LCMAP_SIMPLIFIED_CHINESE, pGBCHTChar, -1, pGBCHSChar, Len * 2);
Result := string(pGBChsChar);
//FreeMem(pGBCHTChar);
FreeMem(pGBCHSChar);
end;

function GBChsToCht(GBStr: string): AnsiString;
{进行GBK简体转繁体}
var
Len: Integer;
pGBCHTChar: PChar;
pGBCHSChar: PChar;
begin
pGBCHSChar := PChar(GBStr);
Len := MultiByteToWideChar(936, 0, pGBCHSChar, -1, nil, 0);
GetMem(pGBCHTChar, Len * 2 + 1);
ZeroMemory(pGBCHTChar, Len * 2 + 1);
//GB CHS -> GB CHT
LCMapString($804, LCMAP_TRADITIONAL_CHINESE, pGBCHSChar, -1, pGBCHTChar, Len * 2);
Result := string(pGBCHTChar);
FreeMem(pGBCHTChar);
//FreeMem(pGBCHSChar);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
edit1.Text := GBChtToChs(Trim(edit1.Text));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
edit1.Text := GBChsToCht(Trim(edit1.Text));
end;

end.
 
有没有在繁体下测试成功过?
 
我用下面这段代码进行测试,要简体系统下运行,可以完整地显示成繁体字,但是在繁体系统下运行时,有的字就是乱码了。
如:Label1.Caption:='中华人民共和国'
在繁体系统下运行显示为:中?人民共和?
function TForm1.GBChsToCht(GBStr: string): AnsiString;
{进行GBK简体转繁体}
var
Len: Integer;
pGBCHTChar: PChar;
pGBCHSChar: PChar;
begin
pGBCHSChar := PChar(GBStr);
Len := MultiByteToWideChar(936, 0, pGBCHSChar, -1, nil, 0);
GetMem(pGBCHTChar, Len * 2 + 1);
ZeroMemory(pGBCHTChar, Len * 2 + 1);
//GB CHS -> GB CHT
LCMapString($804, LCMAP_TRADITIONAL_CHINESE, pGBCHSChar, -1, pGBCHTChar, Len * 2);
Result := string(pGBCHTChar);
FreeMem(pGBCHTChar);
//FreeMem(pGBCHSChar);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
tmpCap: string;
Len: Integer;
begin
Len := Label1.GetTextLen;
SetString(tmpCap, PChar(nil), Len);
Label1.GetTextBuf(Pointer(tmpCap), Len + 1);
Label1.SetTextBuf(PChar(GBChsToCht(tmpCap)));
Label2.Caption:=GBChsToCht(Label2.Caption);
Label1与Label2的Caption是一样的,我只是做为测试而已!
end;
 
function Big52GB(BIG5Str : String): AnsiString;
{进行big5转GB内码}
var
Len: Integer;
pBIG5Char: PChar;
pGBCHSChar: PChar;
pGBCHTChar: PChar;
pUniCodeChar: PWideChar;
begin
//String -> PChar
pBIG5Char := PChar(BIG5Str);
Len := MultiByteToWideChar(950,0,pBIG5Char,-1,nil,0);
GetMem(pUniCodeChar,Len*2);
ZeroMemory(pUniCodeChar,Len*2);
//Big5 -> UniCode
MultiByteToWideChar(950,0,pBIG5Char,-1,pUniCodeChar,Len);
Len := WideCharToMultiByte(936,0,pUniCodeChar,-1,nil,0,nil,nil);
GetMem(pGBCHTChar,Len*2);
GetMem(pGBCHSChar,Len*2);
ZeroMemory(pGBCHTChar,Len*2);
ZeroMemory(pGBCHSChar,Len*2);
//UniCode->GB CHT
WideCharToMultiByte(936,0,pUniCodeChar,-1,pGBCHTChar,Len,nil,nil);
//GB CHT -> GB CHS
LCMapString($804,LCMAP_SIMPLIFIED_CHINESE,pGBCHTChar,-1,pGBCHSChar,Len);
Result := String(pGBCHSChar);
FreeMem(pGBCHTChar);
FreeMem(pGBCHSChar);
FreeMem(pUniCodeChar);
end;

function GB2Big5(GBStr : String): AnsiString;
{进行GB转BIG5内码}
var
Len: Integer;
pGBCHTChar: PChar;
pGBCHSChar: PChar;
pUniCodeChar: PWideChar;
pBIG5Char: PChar;
begin
pGBCHSChar := PChar(GBStr);
Len := MultiByteToWideChar(936,0,pGBCHSChar,-1,nil,0);
GetMem(pGBCHTChar,Len*2+1);
ZeroMemory(pGBCHTChar,Len*2+1);
//GB CHS -> GB CHT
LCMapString($804,LCMAP_TRADITIONAL_CHINESE,pGBCHSChar,-1,pGBCHTChar,Len*2);
GetMem(pUniCodeChar,Len*2);
ZeroMemory(pUniCodeChar,Len*2);
//GB CHT -> UniCode
MultiByteToWideChar(936,0,pGBCHTChar,-1,pUniCodeChar,Len*2);
Len := WideCharToMultiByte(950,0,pUniCodeChar,-1,nil,0,nil,nil);
GetMem(pBIG5Char,Len);
ZeroMemory(pBIG5Char,Len);
//UniCode -> Big5
WideCharToMultiByte(950,0,pUniCodeChar,-1,pBIG5Char,Len ,nil,nil);
Result := String(pBIG5Char);
FreeMem(pBIG5Char);
FreeMem(pGBCHTChar);
FreeMem(pUniCodeChar);
end;

function GBCht2Chs(GBStr : String): AnsiString;
{进行GBK繁体转简体}
var
Len: Integer;
pGBCHTChar: PChar;
pGBCHSChar: PChar;
begin
pGBCHTChar := PChar(GBStr);
Len := MultiByteToWideChar(936,0,pGBCHTChar,-1,nil,0);
GetMem(pGBCHSChar,Len*2+1);
ZeroMemory(pGBCHSChar,Len*2+1);
//GB CHS -> GB CHT
LCMapString($804,LCMAP_SIMPLIFIED_CHINESE,pGBCHTChar,-1,pGBCHSChar,Len*2);
Result := String(pGBChsChar);
//FreeMem(pGBCHTChar);
FreeMem(pGBCHSChar);
end;

function GBChs2Cht(GBStr : String): AnsiString;
{进行GBK简体转繁体}
var
Len: Integer;
pGBCHTChar: PChar;
pGBCHSChar: PChar;
begin
pGBCHSChar := PChar(GBStr);
Len := MultiByteToWideChar(936,0,pGBCHSChar,-1,nil,0);
GetMem(pGBCHTChar,Len*2+1);
ZeroMemory(pGBCHTChar,Len*2+1);
//GB CHS -> GB CHT
LCMapString($804,LCMAP_TRADITIONAL_CHINESE,pGBCHSChar,-1,pGBCHTChar,Len*2);
Result := String(pGBCHTChar);
FreeMem(pGBCHTChar);
//FreeMem(pGBCHSChar);
end;
 
转换好的同时要设置好相应的字库,一般
Font.Name := '宋体';
Font.Charset := GB2312_CHARSET;
Font.Name := 'MS Song';
Font.Charset := CHINESEBIG5_CHARSET;
Font.Name := 'Ш蔨';
Font.Charset := CHINESEBIG5_CHARSET;
 
繁体下面的:‘新细明体’怎么写?
我这样写是不行的:
Font.Name := '新細明體'';
Font.Charset := CHINESEBIG5_CHARSET;
另外,我是先改变字体还是先进行转换?
 
樓上的解決方案可以的,如果用font.charset=:ANSI_CHARSET在上面簡繁體都能正常顯示。
 
我在窗体的初始化时加了改变字体的代码,可还是不行啊!真的好急啊!!!
procedure TForm1.FormCreate(Sender: TObject);
begin
Font.Name := 'MS Song';
Font.Charset := ANSI_CHARSET;
Label2.Caption := GBChs2Cht(Label2.Caption);
Form1.Caption := GBChs2Cht(Form1.Caption);
end;
 
单纯的设置font.charset=ANSI_CHARSET不行的,我刚试过
 
我刚才也试了,也是不行的
procedure TForm1.FormCreate(Sender: TObject);
begin
Font.Charset := CHINESEBIG5_CHARSET;
Font.Name := '新細明體';
Label2.Caption := GBChs2Cht(Label2.Caption);
Form1.Caption := GBChs2Cht(Form1.Caption);
end;
可不可以直接用QQ一起讨论,QQ:492862776
 
Font.Name := 'Ш蔨';
Font.Charset := CHINESEBIG5_CHARSET;
繁体下面, 'Ш蔨'就不是乱码了,变成了繁体的“宋体”
 
后退
顶部