Link to home
Start Free TrialLog in
Avatar of Otis_04
Otis_04

asked on

Exe to binary string

Hey all! I have a small problem.

I need to convert a win32 executable to a binary string, that will be displayed in a memo, in the following format: 00,032,234,123,etc,etc,etc

Could someone help me? If you make a function, where i input the path to the file, and it returns it as one long string, that'd be great. Oh, the max filesize would be around 2/3 MB... But if you can make it work for any file, that'd be great!
Avatar of Kalroth
Kalroth

I just wrote some code that should do it for you.
It's smart in the sense that it doesn't use any buffering, apart from the result string and it should be fairly fast too.

Basically it allocates the string, loads the file in the upper half of the string and then converts the raw data in the upper half to heximal text in the lower half.

Good luck with your project!

(** CODE **)
function FileToHexString(const Filename: String): String;
var
  InputFile : TFileStream;
  InputSize : Integer;
  Middle : PChar;
  Counter : Integer;

  // Taken from KOL, http://xcl.cjb.net/
  function HexDigit( B : Byte ) : Char;
  asm
     CMP  AL,9
     JA   @@1
     ADD  AL,30h-41h+0Ah
  @@1:
     ADD  AL,41h-0Ah
  end;

begin
  if FileExists(Filename) then
  begin
    InputFile := TFileStream.Create(Filename, fmOpenRead);
    try
      InputSize := InputFile.Size;
      // Set length of result, to avoid
      // .. slow dynamic allocation in a loop.
      SetLength(result,InputSize*2);
      // Read the file into the HIGH half
      // .. of the result buffer
      // The buffer will look something like this:
      // |................DATADATADATADATA|
      Middle := @result[InputSize+1];
      InputFile.Read(Middle^,InputSize);
      // Now lets loop through the buffer and
      // .. convert raw data from the middle to
      // .. heximal data in the beginning
      for Counter := 0 to InputSize-1 do
      begin
        result[(Counter*2)+1] := HexDigit((Ord(Middle[Counter]) shr 4) and $F);
        result[(Counter*2)+2] := HexDigit(Ord(Middle[Counter]) and $F);
      end;
    finally
      InputFile.Free;
    end;
  end;
end;
(** /CODE**)
Avatar of Otis_04

ASKER

WOW... Uhm, when i run that, and put the results of converting calculator into a memo, i get this:

ÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏÏ

etc.... theres alot more than that though... I need it to be binary string... I need it to give me the binary string, so i can insert it into a vbs, because the vbs creates a exe from the binary string. Here is the array i need to put it into:

array= array(77,90,68,1,5,0,2,0,32,0,33,0,255,255,117,0,0,2,0,0,153,0,0,0,62,0,0,0... etc)

I want to put the binary string in that array, replacing the old one... maybe if you seperate the values with a comma too, that'd be great.
Avatar of Otis_04

ASKER

Well, i found my wqays in another language, thanks alot though :D
ASKER CERTIFIED SOLUTION
Avatar of SiC_O
SiC_O

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial