★☆★☆★☆★☆★大伙帮忙看看这个Function该怎么改?在线等!!!!
有如下Function,实现的目的是把文件用16进制来表示数据:
function GetHexStringFromFile(const AFileName: string): string;
const
SHex: PChar = '0123456789ABCDEF';
var
P: PChar;
I, Size: Integer;
hFile: Thandle;
V: Byte;
begin
Result := 'NULL';
hFile := FileOpen(AFileName, fmShareDenyNone);
if hFile = INVALID_HANDLE_VALUE then Exit;
try
Size := FileSeek(hFile, 0, FILE_END);
if Size <= 0 then Exit;
FileSeek(hFile, 0, FILE_BEGIN);
SetLength(Result, Size * 2 + 2);
FileRead(hFile, Result[1], Size);
finally
CloseHandle(hFile);
end;
P := PChar(Result) + Length(Result) - 2;
for I := 0 to Size - 1 do
begin
V := Ord(Result[Size - I]);
P[0] := SHex[V shr 4];
P[1] := SHex[V and $F];
Dec(P, 2);
end;
P[0] := '0';
P[1] := 'x';
end; qry.Close;
qry.SQL.Text := Format('Insert into zhouziqiang(name, fdata) Values (''%s'', %s)', [
'abc', GetHexStringFromFile('C:\abc.txt')]);
Qry.ExecSQL ; 我现在要把它变成把内存中的流变成16进制表示,不是文件了 是内存流,请问该如何实现?谢谢!!
把GetHexStringFromFile改成GetHexStringFromStream
有如下Function,实现的目的是把文件用16进制来表示数据:
function GetHexStringFromFile(const AFileName: string): string;
const
SHex: PChar = '0123456789ABCDEF';
var
P: PChar;
I, Size: Integer;
hFile: Thandle;
V: Byte;
begin
Result := 'NULL';
hFile := FileOpen(AFileName, fmShareDenyNone);
if hFile = INVALID_HANDLE_VALUE then Exit;
try
Size := FileSeek(hFile, 0, FILE_END);
if Size <= 0 then Exit;
FileSeek(hFile, 0, FILE_BEGIN);
SetLength(Result, Size * 2 + 2);
FileRead(hFile, Result[1], Size);
finally
CloseHandle(hFile);
end;
P := PChar(Result) + Length(Result) - 2;
for I := 0 to Size - 1 do
begin
V := Ord(Result[Size - I]);
P[0] := SHex[V shr 4];
P[1] := SHex[V and $F];
Dec(P, 2);
end;
P[0] := '0';
P[1] := 'x';
end; qry.Close;
qry.SQL.Text := Format('Insert into zhouziqiang(name, fdata) Values (''%s'', %s)', [
'abc', GetHexStringFromFile('C:\abc.txt')]);
Qry.ExecSQL ; 我现在要把它变成把内存中的流变成16进制表示,不是文件了 是内存流,请问该如何实现?谢谢!!
把GetHexStringFromFile改成GetHexStringFromStream
function GetHexStringFromFile(Stream: TStream): string;
const
SHex: PChar = '0123456789ABCDEF';
var
P: PChar;
I, Size: Integer;
V: Byte;
begin
Result := 'NULL';
Size := Stream.Size;
if Size <= 0 then Exit;
Stream.Position := 0;
SetLength(Result, Size * 2 + 2);
Stream.Read(Result[1], Size);
P := PChar(Result) + Length(Result) - 2;
for I := 0 to Size - 1 do
begin
V := Ord(Result[Size - I]);
P[0] := SHex[V shr 4];
P[1] := SHex[V and $F];
Dec(P, 2);
end;
P[0] := '0';
P[1] := 'x';
end;