您的位置:首页 > 编程语言 > Delphi

delphi十六进制字符串hex转byte数组

2020-06-23 13:09 344 查看 https://blog.csdn.net/sczhf/ar

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;
————————————————
版权声明:本文为CSDN博主「az7772010」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/az7772010/article/details/31405827

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: