Link to home
Create AccountLog in
Avatar of surfbored
surfboredFlag for United States of America

asked on

Convert Hexadecimal Images to Binary Format?

I'm extracting "hexadecimal images" out of RTF documents and I need instructions on how to convert them back to their original format (PNG, JPG, etc.).

Can anyone give me any pointers on how to translate the hex back into binary?

Thanks!

Avatar of developmentguru
developmentguru
Flag of United States of America image

First, how are you extracting the RTF documents?  The term Hexadecimal Images could refer to so many things.  As far as converting to other formats there is a lot of information out there on the conversions.  What is missing here is the starting point.  If you do a screen capture then you are starting with a bitmap.  Give us some more information to work with please.
Avatar of surfbored

ASKER

Sorry for the lack of details. I'm actually reading through the RTF text stream and extracting only the hexadecimal code that I find there.

I've started working on a solution, but I haven't proven that any piece (other than the extraction) actually works. In the code snippet, I have included the two approaches I'm currently trying. Now I'm trying to figure out how to write out the binary data that I *think* I have, to prove that one or both of these are actually working.

Suggestions?
.
.
.
sImg := StringReplace(sImg, #13#10, '',[rfReplaceAll]);
HexDigitToByte(sImg);
iHexReturn := HexToBin3(sImg);
Showmessage(inttostr(iHexReturn));
.
.
.
 
 
//First option
function TForm1.HexDigitToByte(HexData: String): Boolean;
    var
        BinaryData: PByteArray;
        DataSize, I: Integer;
    begin
        BinaryData := nil;
        DataSize := Length(HexData) div 2;
        GetMem(BinaryData, DataSize);
        try
            for I := 0 to DataSize-1 do
                BinaryData[I] := Byte(StrToInt('$' + Copy(HexData, (I*2)+1, 2)));
            // use BinaryData as needed...
        finally
            FreeMem(BinaryData);
        end;
    end;
 
//Second Option
function TForm1.HexToBin3(HexData: string): Integer;
var
  Buf: array[0..SizeOf(HexData) * 2] of Char;
begin
  Result := HexToBin(PAnsiChar(HexData), Buf, SizeOf(Buf) - 1);
end;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of surfbored
surfbored
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer