Solved

Convert Hexadecimal Images to Binary Format?

Posted on 2008-06-11
3
1,195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 21

Expert Comment

by:developmentguru
ID: 21764024
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
ID: 21764640
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
ID: 21765623
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.

726 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