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. Python如何实现doc文件转换为docx文件?

    Python如何实现doc文件转换为docx文件? 在开发过程中遇到一个关于读写doc和docx的问题: 一个文件夹中有两种文件, 一种为doc结尾, 一种为docx结尾, 需要将这些文件全部重命名. ...

  2. LightOJ - 1323 - Billiard Balls(模拟)

    链接: https://vjudge.net/problem/LightOJ-1323 题意: You are given a rectangular billiard board, L and W ...

  3. [NgRx] NgRx Entity Adapter Configuration - Understanding sortComparer and selectId

    import { Course, compareCourses } from "../model/course"; import { EntityState, createEnti ...

  4. GreenPlum 常用命令

    gpstate 命令 参数 作用 gpstate -b => 显示简要状态 gpstate -c => 显示主镜像映射 gpstart -d => 指定数据目录(默认值:$MASTE ...

  5. Bzoj 2875: [Noi2012]随机数生成器(矩阵乘法)

    2875: [Noi2012]随机数生成器 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 2052 Solved: 1118 Description ...

  6. AtCoder Grand Contest 010题解

    传送门 \(A\) 判一下奇数的个数就行了 const int N=1e5+5; int a[N],n,res; int main(){ scanf("%d",&n); f ...

  7. 《挑战30天C++入门极限》新手入门:关于C++中的内联函数(inline)

        新手入门:关于C++中的内联函数(inline) 在c++中,为了解决一些频繁调用的小函数大量消耗栈空间或者是叫栈内存的问题,特别的引入了inline修饰符,表示为内联函数. 可能说到这里,很 ...

  8. fork()函数 图解

    code #include<stdio.h> #include <getopt.h> #include<iostream> #include<string&g ...

  9. Docker理论简答

    Docker理论简答: 1.        介绍对docker的认识(10分) Docker是容器,容器不是docker Dockers就是一个文件夹,它欺骗操作系统说自己是一个操作系统,然后把所需要 ...

  10. 利用python的matplotlib处理计算数据

    #!/usr/bin/python # -*- coding: UTF-8 -*- import numpy as np import matplotlib.pyplot as plt import ...