16,748
社区成员
发帖
与我相关
我的任务
分享
String s = new String(org.apache.commons.codec.binary.Hex.encodeHex(byte[] b));
TByteArray2 = array of Byte;
var
buff : TByteArray2;
P : PByte;
P_SendStream: TMemoryStream;
BufSize : Cardinal;
begin
P_SendStream := TMemoryStream.Create();
try
P_SendStream.LoadFromFile('d:\a.zip');
BufSize := P_SendStream.Size;
SetLength(buff,BufSize);
P_SendStream.Position := 0;
P_SendStream.ReadBuffer(buff,BufSize);//必然出错,为什么?
mmo1.Clear;
mmo1.Lines.Add(LowerCase( BufferToHex(buff,BufSize)));
except
FreeAndNil(P_SendStream);
end;
end;
{ -------------------------------------------------------------------------- }
function BufferToHex(const Buf; BufSize : Cardinal) : string;
var
I : LongInt;
begin
Result := '';
for I := 0 to BufSize - 1 do
Result := Result + IntToHex(TByteArray(Buf)[I], 2); {!!.01}
end;
{ -------------------------------------------------------------------------- }
function HexToBuffer(const Hex : string; var Buf; BufSize : Cardinal) : Boolean;
var
i, C : Integer;
Str : string;
Count : Integer;
begin
Result := False;
Str := '';
for i := 1 to Length(Hex) do
if Upcase(Hex[i]) in ['0'..'9', 'A'..'F'] then
Str := Str + Hex[i];
FillChar(Buf, BufSize, #0);
Count := Min(Length(Hex), BufSize);
for i := 0 to Count - 1 do begin
Val('$' + Copy(Str, (i shl 1) + 1, 2), TByteArray(Buf)[i], C); {!!.01}
if (C <> 0) then
Exit;
end;
Result := True;
end;