因ANSI的文本文件在别的浏览器里面乱码,只能转换成为UNICODE文本。
别人C++里面已经实现了,但是delphi里面我还是搞不定,希望有高手帮我指点指点
C++中代码如下void CINetCtrl::ConvertTxtFileToUnicode()
{
CFile file1, file2;
file1.Open( "c:\123.text",CFile::modeRead );
file2.Open( "c:\456.text",CFile::modeCreate | CFile::modeWrite ); file2.Write( "\xFF\xFE", 2 ); int m = file1.GetLength(); CHAR * buf;
int n;
WCHAR * bufw; buf = new CHAR [m];
file1.Read( buf, m );
n = MultiByteToWideChar( CP_ACP, 0, buf, m, 0, 0 );
bufw = new WCHAR [n];
MultiByteToWideChar( CP_ACP, 0, buf, m, bufw, n ); file2.Write( bufw, n*sizeof(WCHAR) ); delete [] buf;
delete [] bufw; file1.Close();
file2.Close();
}在delphi里面转换ANSI代码成UNICODE倒是可以了,但是怎么去写这个"\xFF\xFE"的UNSICODE文件的头我就不知道了,还有怎么往文件里面写内容呢?
别人C++里面已经实现了,但是delphi里面我还是搞不定,希望有高手帮我指点指点
C++中代码如下void CINetCtrl::ConvertTxtFileToUnicode()
{
CFile file1, file2;
file1.Open( "c:\123.text",CFile::modeRead );
file2.Open( "c:\456.text",CFile::modeCreate | CFile::modeWrite ); file2.Write( "\xFF\xFE", 2 ); int m = file1.GetLength(); CHAR * buf;
int n;
WCHAR * bufw; buf = new CHAR [m];
file1.Read( buf, m );
n = MultiByteToWideChar( CP_ACP, 0, buf, m, 0, 0 );
bufw = new WCHAR [n];
MultiByteToWideChar( CP_ACP, 0, buf, m, bufw, n ); file2.Write( bufw, n*sizeof(WCHAR) ); delete [] buf;
delete [] bufw; file1.Close();
file2.Close();
}在delphi里面转换ANSI代码成UNICODE倒是可以了,但是怎么去写这个"\xFF\xFE"的UNSICODE文件的头我就不知道了,还有怎么往文件里面写内容呢?
解决方案 »
- 请教关于获取指定应用程序窗口内文本的方法
- 哪里有调用web sercices的例子
- ACCESS中存入Icon文件,取出后图像严重失真
- 用Access 做数据库,是不是WIN98以上的每个系统?
- FastReport 是什么好控件
- 急,高分求救,delphi中sql中建表出错,散分求救
- 关于用filter进行查询的问题
- 请问如何在运行时动态新建一个文件
- 调用出错,Types of actual and formula var parameters must be identical.
- 如何将数据库中的数据导出到excel中阿?
- 关于mpeg4压缩时出现问题,运行时提示Invalid floating point operation
- 菜鸟问题一个:验证窗体(普通)、主窗体(MDI)、子窗体1(MDICHILD1) 启动顺序问题!分不够另开贴再加!!!!
干脆哪位高手帮我把那段C++的代码改成delphi吧,多谢多谢!我再加100分相赠!
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Db, DBTables, FileCtrl, ComCtrls, Registry,
Grids, DBGrids, Psock, NMFtp,shellapi;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
function AnsiToUnicode(Ansi: string):string;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
var
file1,file2:FILE;
flength,SizeOfFile:int64;
len,n:integer;
s,unis:string;
Buf: array[1..255] of Char;
NumRead, NumWritten: Integer;begin
AssignFile(file1,'d:\123.txt');
AssignFile(file2,'d:\456.txt'); //FileMode:=0;
Reset(file1);
rewrite(file2);
//n:= MultiByteToWideChar( CP_ACP, 0, buf, SizeOfFile, 0, 0 );
//buf[1]:=FF;
// Write(file2,$FFFE);
//buf:='255';
// buf:='';
S:='YOU FASER!';
strPCopy(BUF,S); BlockWrite(file2,buf,4);
{ Canvas.TextOut(10, 10, 'Copying ' + IntToStr(FileSize(file1)) + ' bytes...');
flength:= SizeOf(Buf) ;
repeat
BlockRead(file1,buf,SizeOf(Buf), NumRead);
BlockWrite(file2,Buf, NumRead, NumWritten);
until (NumRead = 0) or (NumWritten <> NumRead);
CloseFile(file1);
CloseFile(file2); }
// len := Length('\xFF\xFE');
// Write( file2,\xFF\xFE');
{ While Not Eof(file1) do
begin
readln(file1,s);
len := Length(s); unis:=(AnsiToUnicode(s)); Write(file2, PChar(unis)); // SizeOfFile:=FileSize(file1);
// showmessage(inttostr(SizeOfFile));
end;}
CloseFile(file1);
CloseFile(file2);end;function TForm1.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;
end.
下面的代码好歹算是解决了,就是速度很慢而且还有问题,不过去浏览倒是能ok了
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
writechar:array [0..60000] of char;
thechar:array [0..60000] of char;
procedure AnsiToUnicode(Ansi:string);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
var
file2:file;
file1:file;
i:integer;
NumRead, NumWritten: longint;
rec : array[0..60000] of char;
res: string;
buffer : array[0..32] of byte;
psition:integer;
begin
AssignFile(file1,'d:\123.txt');
AssignFile(file2,'d:\456.txt'); Reset(file1,1);
Seek(file1, 0);
psition:=0;
rewrite(file2,1);
buffer[0] := byte($FF);
buffer[1] := byte($FE);
BlockWrite(file2, buffer[0],1);
BlockWrite(file2, buffer[1],1); try
repeat
Seek(file1, psition);
BlockRead(file1, rec, SizeOf(rec), NumRead); psition:=NumRead+psition;
res:=rec;
AnsiToUnicode(res) ; for i:=0 to sizeof(thechar) do
begin
thechar[i]:=writechar[i+1];
thechar[i+1]:=writechar[i]; end;
BlockWrite(file2,thechar,NumRead*2, NumWritten);
until (NumRead = 0) or (NumWritten <> NumRead); except
CloseFile(file1);
CloseFile(file2);
close;
end; CloseFile(file1);
CloseFile(file2); Close;
end;procedure TForm1.AnsiToUnicode(Ansi: string);
var
s:string;
i:integer;
j,k:string[2];
begin
//s:='';
StringToWideChar(Ansi,@(writechar[1]),65535);
i:=1;
while ((writechar[i]<>#0) or (writechar[i+1]<>#0)) do begin
j:=IntToHex(Integer(writechar[i]),2);
k:=IntToHex(Integer(writechar[i+1]),2);
s:=s+j+k; i:=i+2;
end;
end;end.