procedure Hex2Png(str: string; out png: TPngObject);
var stream: TMemoryStream;
begin
if not Assigned(png) then png := TPngObject.Create;
stream := TMemoryStream.Create;
stream.SetSize(Length(str) div );
HexToBin(PChar(str), stream.Memory, stream.Size);
png.LoadFromStream(stream);
stream.Free;
end;
function Png2Hex(png: TPngObject): string;
var stream: TMemoryStream;
begin
stream := TMemoryStream.Create;
png.SaveToStream(stream);
SetLength(Result, stream.Size * );
BinToHex(stream.Memory, PChar(Result), stream.Size);
stream.Free;
end;
function bmp2Hex(out bmp: TBitmap):string;
var stream: TMemoryStream;
begin
stream := TMemoryStream.Create;
bmp.SaveToStream(stream);
SetLength(Result, stream.Size * );
BinToHex(stream.Memory, PChar(Result), stream.Size);
stream.Free;
end;
procedure Hex2bmp(str: string; out bmp: TBitmap);
var stream: TMemoryStream;
begin
if not Assigned(bmp) then bmp := TBitmap.Create;
stream := TMemoryStream.Create;
stream.SetSize(Length(str) div );
HexToBin(PChar(str), stream.Memory, stream.Size);
bmp.LoadFromStream(stream);
stream.Free;
end;
//十六进制字符串转入byte数组 1
function Hex2Byte(sRandomPwd:String):TByteArr;
var
Buf:TByteArr;
begin
SetLength(Buf, Length(sRandomPwd) div );
// HexToBin(PAnsiChar(sRandomPwd), @Buf[0], Length(sRandomPwd) div 2);
//HexToBin(PChar(sRandomPwd), @Buf[0], Length(sRandomPwd) div 2);
HexToBin(PAnsiChar(sRandomPwd), @Buf[], Length(sRandomPwd) div );
Result:=buf;
end;
//自定义函数,转换十六进制数为十进制数
function HexToInt(Hexa: string): LongWord;
const
ValoresHexa: array['A'..'F'] of integer = (, , , , , );
var
nDecimal: LongWord;
nIndex: byte;
begin
nDecimal := ;
Hexa := Uppercase(Hexa);
for nIndex := Length(Hexa) downto do
if Hexa[nIndex] in [''..'']
then nDecimal := nDecimal + StrToInt(Hexa[nIndex]) *
Trunc(Exp((Length(Hexa) - nIndex) * ln()))
else nDecimal := nDecimal + ValoresHexa[Hexa[nIndex]] *
Trunc(Exp((Length(Hexa) - nIndex) * ln()));
Result := nDecimal;
end;
//十六进制字符串转入byte数组 2
function Hex2Byte2(sRandomPwd:String):TByteArr;
var
Buf:TByteArr;
i:Integer;
begin
sRandomPwd := StringReplace(sRandomPwd,' ', '',[]);
if ((Length(sRandomPwd) mod ) <> ) then
begin
sRandomPwd := sRandomPwd+' ';
end;
i:= Trunc(Length(sRandomPwd)/);
SetLength(Buf,i);
for i := to Length(Buf)- do
begin
Buf[i]:=HexToInt(Copy(sRandomPwd,i*+,));
end;
Result:=Buf;
end;
//byte数组转入十六进制字符串1
function Byte2Hex(arrByte:TByteArr):string;
var stream: TMemoryStream;
begin
stream := TMemoryStream.Create;
stream.Write(arrByte[],Length(arrbyte));
SetLength(Result, stream.Size * );
BinToHex(stream.Memory, PChar(Result), stream.Size);
stream.Free;
end;
//byte数组转入十六进制字符串2
function Byte2Hex2(arrByte:TByteArr):string;
var
i:integer;
begin
for i:= to length(arrByte)- do
begin
Result := Result + Copy( IntToHex(arrByte[i],),,)
end;
end; procedure TForm1.btnHex2PngClick(Sender: TObject);
var png: TPngObject;
begin
png := TPngObject.Create;
Hex2Png(Memo1.Text, png);
Canvas.Draw(, , png);
png.Free;
end;
procedure TForm1.btnPng2HexClick(Sender: TObject);
var png: TPngObject;
begin
png := TPngObject.Create;
png.LoadFromFile('dyc.png');
Memo1.Text := Png2Hex(png);
png.Free;
end;
procedure TForm1.btnBmp2HexClick(Sender: TObject);
var bmp: TBitmap;
begin
image1.Picture.Bitmap.SaveToFile('2.bmp');
bmp := TBitmap.Create;
bmp.LoadFromFile('2.bmp');
Memo1.Text := bmp2Hex(bmp);
bmp.Free;
end ; procedure TForm1.btnHex2BmpClick(Sender: TObject);
var bmp: tbitmap;
begin
bmp := tbitmap.Create;
Memo1.Text:=StringReplace(Memo1.Text,#,'',[]);
Memo1.Text:=StringReplace(Memo1.Text,#,'',[]);
Hex2bmp(Memo1.Lines.GetText,bmp);
Canvas.Draw(, , bmp);
bmp.Free;
end; procedure TForm1.btnByte2HexClick(Sender: TObject);
var
m:TMemoryStream;
b:TByteArr;
begin
m:=TMemoryStream.Create();
Image1.Picture.Bitmap.SaveToStream(m);
m.Position:=;
SetLength(b,m.size);
m.ReadBuffer(b[],m.Size);
Memo1.Text:=Byte2Hex2(b);//或者Memo1.Text:=Byte2Hex2(b);
end; procedure TForm1.btnHex2ByteClick(Sender: TObject);
var
m:TMemoryStream;
b:TByteArr;
bmp:TBitmap;
begin
b:=Hex2Byte2(Memo1.text);//或者Hex2Byte2(Memo1.text);
m:=TMemoryStream.Create;
m.WriteBuffer(b[],Length(b));
m.Position:=;
m.Seek(,soBeginning);
bmp:=TBitmap.Create;
bmp.LoadFromStream(m);
Canvas.Draw(,,bmp);
end;