function ZsStringToFile(mString: string; mFileName: TFileName): Boolean; { 字符串保存到文件 } var vFile: file of Char; I: Integer; begin {$I-} AssignFile(vFile, mFileName); Rewrite(vFile); for I := 1 to Length(mString) do Write(vFile, mString[I]); CloseFile(vFile); {$I+} Result := (IOResult = 0) and (mFileName <> ''); end; { ZsStringToFile }function ZsFileToString(mFileName: TFileName): string; { 从文件载入字符串 } var vFile: file of Char; vChar: Char; begin Result := ''; {$I-} AssignFile(vFile, mFileName); Reset(vFile); while not Eof(vFile) do begin Read(vFile, vChar); Result := Result + vChar; end; CloseFile(vFile); {$I+} end; { ZsFileToString }
//把二进制变成可视字符(十六进制)function _strtohex(const s:string):string; var iCount:integer; Value,CH,CL:Byte; begin SetLength(Result,2 * Length(s)); for iCount := 1 to Length(s) do begin Value := Byte(S[iCount]); CH:=(Value shr 4) and $0F; CL:=Value and $0F; if CL < $0A then CL := CL + $30 else CL := CL + $37; if CH < $0A then CH := CH + $30 else CH := CH + $37; Result[iCount * 2 - 1]:= Char(CH); Result[iCount * 2] := Char(CL); end; end; function binToStr(fileName:string):string; var MS:TMemoryStream; S:string; begin //读任意文件到字符串中 MS := TMemoryStream.Create(); MS.LoadFilename(FileName); SetLength(S,MS.Size); Move(MS.Memory^,Pointer(S)^,MS.Size); MS.Free(); //变成可视字符 Result := _strtohex(S); end;//范例 var s:string; begin S := binToStr('C:\XXX.Bmp'); showMessage(S); end;
{ 字符串保存到文件 }
var
vFile: file of Char;
I: Integer;
begin
{$I-}
AssignFile(vFile, mFileName);
Rewrite(vFile);
for I := 1 to Length(mString) do Write(vFile, mString[I]);
CloseFile(vFile);
{$I+}
Result := (IOResult = 0) and (mFileName <> '');
end; { ZsStringToFile }function ZsFileToString(mFileName: TFileName): string;
{ 从文件载入字符串 }
var
vFile: file of Char;
vChar: Char;
begin
Result := '';
{$I-}
AssignFile(vFile, mFileName);
Reset(vFile); while not Eof(vFile) do begin
Read(vFile, vChar);
Result := Result + vChar;
end;
CloseFile(vFile);
{$I+}
end; { ZsFileToString }
var
iCount:integer;
Value,CH,CL:Byte;
begin
SetLength(Result,2 * Length(s));
for iCount := 1 to Length(s) do
begin
Value := Byte(S[iCount]);
CH:=(Value shr 4) and $0F;
CL:=Value and $0F;
if CL < $0A then CL := CL + $30 else CL := CL + $37;
if CH < $0A then CH := CH + $30 else CH := CH + $37;
Result[iCount * 2 - 1]:= Char(CH);
Result[iCount * 2] := Char(CL);
end;
end;
function binToStr(fileName:string):string;
var
MS:TMemoryStream;
S:string;
begin
//读任意文件到字符串中
MS := TMemoryStream.Create();
MS.LoadFilename(FileName);
SetLength(S,MS.Size);
Move(MS.Memory^,Pointer(S)^,MS.Size);
MS.Free();
//变成可视字符
Result := _strtohex(S);
end;//范例 var
s:string;
begin
S := binToStr('C:\XXX.Bmp');
showMessage(S);
end;
你的可以了,那反过来怎么做?
function _HexStrToStr(const HexStr:string):string;//与_strtohex 配和使用
const
NumericBin :array[$30..$39] of Byte = (0,1,2,3,4,5,6,7,8,9);
CharBin :array[$41..$46] of Byte = (10,11,12,13,14,15);
var
BH,BL:Byte;
iCount:integer;
P:^Byte;
begin
SetLength(Result,Length(HexStr) div 2);
P := Pointer(Result);
for iCount := 0 to Length(HexStr) div 2 - 1 do
begin
BH := Byte(HexStr[iCount * 2 + 1]);
if BH >= $41 then BH := CharBin[BH] else BH := NumericBin[BH];
BL := Byte(HexStr[iCount * 2 + 2]);
if BL >= $41 then BL := CharBin[BL] else BL := NumericBin[BL];
BH := BH shl 4 + BL and $0F;
P^ := BH;
Inc(P);
end;
end;