• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

delphi十六进制字符串hex转byte数组互相转换bmp图片

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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 2);
  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 * 2);
  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 * 2);
  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 2);
   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 2);
  // HexToBin(PAnsiChar(sRandomPwd), @Buf[0], Length(sRandomPwd) div 2);
   //HexToBin(PChar(sRandomPwd), @Buf[0], Length(sRandomPwd) div 2);
   HexToBin(PAnsiChar(sRandomPwd), @Buf[0], Length(sRandomPwd) div 2);
   Result:=buf;
end;
//自定义函数,转换十六进制数为十进制数
function HexToInt(Hexa: string): LongWord;
const
  ValoresHexa: array['A'..'F'] of integer = (10, 11, 12, 13, 14, 15);
var
  nDecimal: LongWord;
  nIndex: byte;
begin
  nDecimal := 0;
  Hexa := Uppercase(Hexa);
  for nIndex := Length(Hexa) downto 1 do
  if Hexa[nIndex] in ['0'..'9']
  then nDecimal := nDecimal + StrToInt(Hexa[nIndex]) *
  Trunc(Exp((Length(Hexa) - nIndex) * ln(16)))
  else nDecimal := nDecimal + ValoresHexa[Hexa[nIndex]] *
  Trunc(Exp((Length(Hexa) - nIndex) * ln(16)));
  Result := nDecimal;
end;
//十六进制字符串转入byte数组 2
function Hex2Byte2(sRandomPwd:String):TByteArr;
var
  Buf:TByteArr;
  i:Integer;
begin
  sRandomPwd := StringReplace(sRandomPwd,' ', '',[]);
  if ((Length(sRandomPwd) mod 2) <> 0)    then
  begin
    sRandomPwd := sRandomPwd+' ';
  end;
  i:= Trunc(Length(sRandomPwd)/2);
  SetLength(Buf,i);
  for i := 0 to Length(Buf)-1 do
  begin
    Buf[i]:=HexToInt(Copy(sRandomPwd,i*2+1,2));
  end;
  Result:=Buf;
end;
//byte数组转入十六进制字符串1
function Byte2Hex(arrByte:TByteArr):string;
var  stream: TMemoryStream;
begin
  stream := TMemoryStream.Create;
  stream.Write(arrByte[0],Length(arrbyte));
  SetLength(Result, stream.Size * 2);
  BinToHex(stream.Memory, PChar(Result), stream.Size);
  stream.Free;
end;
//byte数组转入十六进制字符串2
function Byte2Hex2(arrByte:TByteArr):string;
var
  i:integer;
begin
  for i:=0 to length(arrByte)-1 do
  begin
    Result := Result + Copy( IntToHex(arrByte[i],16),15,2)
  end;
end;

procedure TForm1.btnHex2PngClick(Sender: TObject);
var  png: TPngObject;
begin
  png := TPngObject.Create;
  Hex2Png(Memo1.Text, png);
  Canvas.Draw(0, 0, 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,#13,'',[]);
  Memo1.Text:=StringReplace(Memo1.Text,#10,'',[]);
  Hex2bmp(Memo1.Lines.GetText,bmp);
  Canvas.Draw(1, 1, 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:=0;
  SetLength(b,m.size);
  m.ReadBuffer(b[0],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[0],Length(b));
  m.Position:=0;
  m.Seek(0,soBeginning);
  bmp:=TBitmap.Create;
  bmp.LoadFromStream(m);
  Canvas.Draw(0,0,bmp);
end;

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
matlab程序里调用python文件发布时间:2022-07-18
下一篇:
切片操作:MATLABVSPython发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap