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;

delphi十六进制字符串hex转byte数组互相转换bmp图片的更多相关文章

  1. 十六进制字符串转化为byte数组

    工作上有这样的需求之前找了好多都不行,好不容易有个可以的赶紧留下来. 原址:http://blog.163.com/roadwalker@126/blog/static/113561841201013 ...

  2. 图片和base64编码字符串 互相转换,图片和byte数组互相转换

    图片和base64编码字符串 互相转换 import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.io.*; ...

  3. Java 图片与byte数组互相转换

    //图片到byte数组 public byte[] image2byte(String path){ byte[] data = null; FileImageInputStream input = ...

  4. c# string 和 byte[]数组之间转换

    在文件流读取和存储过程当中,经常涉及到byte[]数组形式存储数据,再此过程中也涉及到String类型字符串和byte[]的类型转换,下面我们举例说明一下. 现在有一个字符串: string str ...

  5. 一次面试题,将 字符串 保存在 Byte 数组中

    最近在面试,遇到一个面试题 字符串 String str = "AD428C93DE" 编程实现把 str 的内容放到 Byte[6] b 的数组中,存入后并能恢复原来的字符串. ...

  6. C# 文件转byte数组,byte数组再转换文件

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  7. C# String与Byte数组的转换

    string转byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes(str); byte[] byteArray = Enc ...

  8. java 实现 图片与byte 数组互相转换

    package webgate; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import ja ...

  9. c#image与byte数组的转换

    // image to byte[] Image photo = new Bitmap(selectPictureFile); System.IO.MemoryStream ms = new Syst ...

随机推荐

  1. Java中装箱和拆箱的代码

    建议使用1.5或以上的jdk运行, //装箱  值类型到引用类型  int i = 10;  Object object =i;  System.out.println(object);      / ...

  2. javaweb学习笔记(二)

    一.javaweb学习是所需要的细节 1.Cookie的注意点 ① Cookie一旦创建,它的名称就不能更改,Cookie的值可以为任意值,创建后允许被修改. ② 关于Cookie中的setMaxAg ...

  3. 洛谷 P1140 相似基因 题解

    每日一题 day23 打卡 Analysis dp[i][j]表示序列A中前i个与序列B中前j个匹配的相似度最大值 所以,dp方程很容易想到: 1.让a[i]与b[j]匹配 2.让a[i]与B序列中一 ...

  4. Logparser

    http://www.microsoft.com/en-us/download/details.aspx?id=24659 Logparser 的用法 https://www.cnblogs.com/ ...

  5. 通过django-crontab扩展来实现 定时任务

    pip install django-crontab 基本格式 : * * * * * 分 时 日 月 周 命令 M: 分钟(0-59).每分钟用*或者 */1表示 H:小时(0-23).(0表示0点 ...

  6. loj #137 and #6021

    最小瓶颈路 加强版 重构树 最小生成树在合并 (x, y) 时,新建节点 z,link(x, z), link(y, z), 新建节点的权值为 w_{x,y}, 这样的 话任意两点的 answer 为 ...

  7. BFC、IFC、FFC、GFC

    FC(Formatting Context) 它是W3C CSS2.1规范中的一个概念,定义的是页面中的一块渲染区域,并且有一套渲染规则,它决定了其子元素将如何定位,以及和其他元素的关系和相互作用. ...

  8. [DOM] Input elements should have autocomplete attributes (suggested: autocomplete='tel', confirm at

    译文概要:输入元素应该有自动完成的属性,比如: autocomplete=’tel’. autocomplete 用途: 此功能主要是记住输入内容,下次提交表单或者浏览器回退后,还能保持表单内容不变. ...

  9. Note_4.7

    2019/4/7 奇奇怪怪的笔记 狄利克雷卷积  \(μ∗1=ϵ\),莫比乌斯反演 \(Id=φ∗1⇒φ=μ∗Id\) \(d=1∗1⇒1=μ∗d\) \(σ=Id∗1⇒Id=μ∗σ\) \(σ=φ∗ ...

  10. 扩展ACL