这个问题我自己已经解决。利用的是最笨的办法,每次读来的数据都进行一次的逆转
首先,在单元的初始化的位置,加入一个判断。看是否需要逆转buffer的顺序。
Const
TestStr = 'CAFEBABE';
//在磁盘上字符串JAVA对应的存储二进制码应该是4341464542414145
var
FileName:String;
MyFile:TextFile;
ThisInt:Int64;
TempStream:TFileStream;
initialization
{ Initialization section goes here }
begin
fileName := ExtractFilePath(Application.ExeName)+'test.$$$';
AssignFile(MyFile,FileName);
Rewrite(MyFile);
Writeln(MyFile,TestStr);
CloseFile(MyFile);
TempStream:= TFileStream.Create(FileName,fmOpenRead );
TempStream.ReadBuffer(ThisInt,4);
case ThisInt of
$46454341{FECA},$45464143{EFAC}:
begin
{$DEFINE NeedTranslate};
//Application.MessageBox('反了','');
YouCanSee:=False;
end;
//$4341464542414145 :
$43414645:
begin
//Application.MessageBox('好的','');
YouCanSee:=True;
end;
else
{$DEFINE UnIdenticalOS}
//Application.MessageBox('惨了','');
end;
//{$IFDEF NeedTranslate}
// Application.MessageBox('','');
//{$ENDIF}
TempStream.Free;
DeleteFile(FileName);
end;
然后,自己写了这样的一段代码
function classfile.ReverseBuffer(Source: Int64): Int64;
var
thisBuffer:Int64;
TempRes:Int64;
begin
if YouCanSee then
begin
Result := Source;
end
else
begin
thisBuffer := Source;
TempRes:= 0;
while thisBuffer >255 do
begin
TempRes := TempRes * 256 + thisBuffer mod 256;
thisBuffer := thisBuffer div 256;
//Application.MessageBox(pchar(InttoHex(TempRes,4)),'');
end;
TempRes := TempRes*256 + thisBuffer;
Result := TempRes;
end;
end;
function classfile.ReverseBuffer(Source: Integer): Integer;
var
thisBuffer:LongInt;
thisTempRes:LongInt;
begin
if YouCanSee then
begin
Result := Source;
end
else
begin
thisBuffer := Source;
thisTempRes := 0;
while thisBuffer >255 do
begin
thisTempRes := thisTempRes * 256 + thisBuffer mod 256;
thisBuffer := thisBuffer div 256;
end;
thisTempRes := thisTempRes + thisBuffer;
Result := thisTempRes;
end;
end;
在每一个读入的字节流后,都进行逆转,很笨