Solved

Convert Hexadecimal Images to Binary Format?

Posted on 2008-06-11
3
1,192 Views
Last Modified: 2008-06-19
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!

0
Comment
Question by:surfbored
  • 2
3 Comments
 
LVL 21

Expert Comment

by:developmentguru
Comment Utility
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.
0
 
LVL 1

Author Comment

by:surfbored
Comment Utility
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

0
 
LVL 1

Accepted Solution

by:
surfbored earned 0 total points
Comment Utility
Sorry for the "tease", Team B gave me the following solution!


function TForm1.HexToBin4(HexData: string): Integer;

var

  Buf: PChar;

  BufSize: Integer;

  fs: TFileStream;

begin

  BufSize := Length(HexData) div 2;

  GetMem(Buf, BufSize);

    try

      Result := HexToBin(PChar(HexData), Buf, BufSize);

      fs := TFileStream.Create('c:\temp\test.jpg', fmCreate);

        try

          fs.WriteBuffer(Buf^, BufSize);

        finally

          fs.Free;

        end;

    finally

      FreeMem(Buf);

    end;

end;

Open in new window

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Downloading email attachments 2 53
Word 2013/2016 Saving as Temp File 9 32
Multiple image collision 13 42
Convert .PDF 6 37
I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now