?
Solved

image to hex and hex to image

Posted on 2008-11-13
7
Medium Priority
?
3,548 Views
Last Modified: 2012-08-13
hello experts!
can someone help me with converting image to hexadecimal and hexadecimal to image?
like at the .dfm..

thanks
0
Comment
Question by:controlr
  • 3
  • 2
  • 2
7 Comments
 
LVL 28

Expert Comment

by:2266180
ID: 22952734
it's not exactly hex. it's some coding, like base64. see attached code.
// following 2 functions taken from http://www.swissdelphicenter.ch/en/showcode.php?id=1524
const
  Codes64 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/';
 
function Encode64(S: string): string;
var
  i: longint;
  a: Integer;
  x: Integer;
  b: Integer;
begin
  Result := '';
  a := 0;
  b := 0;
  for i := 1 to Length(s) do
  begin
    x := Ord(s[i]);
    b := b * 256 + x;
    a := a + 8;
    while a >= 6 do
    begin
      a := a - 6;
      x := b div (1 shl a);
      b := b mod (1 shl a);
      Result := Result + Codes64[x + 1];
    end;
  end;
  if a > 0 then
  begin
    x := b shl (6 - a);
    Result := Result + Codes64[x + 1];
  end;
end;
 
function Decode64(S: string): string;
var
  i: longint;
  a: Integer;
  x: Integer;
  b: Integer;
begin
  Result := '';
  a := 0;
  b := 0;
  for i := 1 to Length(s) do
  begin
    x := Pos(s[i], codes64) - 1;
    if x >= 0 then
    begin
      b := b * 64 + x;
      a := a + 6;
      if a >= 8 then
      begin
        a := a - 8;
        x := b shr a;
        b := b mod (1 shl a);
        x := x mod 256;
        Result := Result + chr(x);
      end;
    end
    else
      Exit;
  end;
end;

Open in new window

0
 

Author Comment

by:controlr
ID: 22952820
nice but i cant put an image on it :(
i want to convert image to hexdecimal..
put an image on a form (with a pic inside) and save it then open the .dfm file
go to picture.data and you will understand what im looking for.

and thanks for the comment :)
0
 
LVL 28

Expert Comment

by:2266180
ID: 22953518
if you are trying to read from a dfm, why not use the delphi standard component streaming mechanism?

you read up the dfm, extract the image part, save it into a stringstream and then load the timage from it.

is this what you are trying to accomplish? if not, what? because you said "like dfm".  I need to know exactly in order to give exact code.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:controlr
ID: 22953859
yeah something like that but i want to do it at the code.
for example:
i have a pick a jpg file with the open dialog
and then in the code i convert it to hexadecimal (all the jpg is now just a big block of numbers)
0
 
LVL 13

Expert Comment

by:ThievingSix
ID: 22954376
Uhh like this?

function FileToHex(FileName: String): String;
var
  F : DWORD;
  Buffer : PChar;
  FileSize : DWORD;
  BytesRead : DWORD;
  I : DWORD;
begin
  Result := '';
  F := CreateFile(PChar(FileName),GENERIC_READ,0,nil,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN);
  If F = INVALID_HANDLE_VALUE Then Exit;
  Try
    FileSize := SetFilePointer(F,0,nil,FILE_END);
    If FileSize = 0 Then Exit;
    SetFilePointer(F,0,nil,FILE_BEGIN);
    Buffer := AllocMem(FileSize);
    If Buffer = nil Then Exit;
    Try
      ReadFile(F,Buffer^,FileSize,BytesRead,nil);
      If BytesRead <> FileSize Then Exit;
      For I := 0 To BytesRead Do
        begin
        Result := Result + IntToHex(Byte(Buffer^),2);
        Inc(Buffer);
      end;
    Finally
      FreeMem(Buffer);
    end;
  Finally
    CloseHandle(F);
  end;
end;

Open in new window

0
 
LVL 28

Expert Comment

by:2266180
ID: 22954395
and what do you do with that "hexadecimal" string? (because I have the feeeling that you don't really want that delphi streaming thing, but that base64 thing I posted earlier)
0
 
LVL 13

Accepted Solution

by:
ThievingSix earned 1500 total points
ID: 22954440
Opps

F := CreateFile(PChar(FileName),GENERIC_READ,0,nil,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,0);


and

      For I := 0 To BytesRead - 1 Do
        begin
        Result := Result + IntToHex(Byte(Buffer[I]),2);
      end;
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

850 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