Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

BIN to HEX

by drip (Beadle)
on Aug 22, 2006 at 06:50 UTC ( [id://568784]=perlquestion: print w/replies, xml ) Need Help??

drip has asked for the wisdom of the Perl Monks concerning the following question:

i am new to perl and i have a lot of questions..one of those is this:

Example i have a binary file that contains this:

MZ´^@^L^@^@^@ ^@^@^@ÿÿ^G^@^@^Ae@^@^@^@^@@^@^@^@^A^@^@^@^@^@^@^@^@^@^@^ +@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^D^@^@^@^@^@^@^@^@^@^ $ Z^N^_´ Í!¸^ALÍ!^@^@^@^@^@^@^@ +^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@ :^Ac^FAf^^Õ^E^P^@^@^H^@^@^@^@^O^@^@^@^@^@^P^@^D^@b^K@^@À^@^K^A^[^A#^A^ +K^@^@^C^A ^@^@^@^B^H^L^@^N^@^@^@ ^C^M^@^N^A@^M^N^A^Z^@d^O^P^]d^O#^@qX^P^]rXQ^@M^P^]My^@$^C^P^]$^C{^@Å^D +^P^]Æ^D~^@ö ^P^]ö ^@^D^G^P^]^D^G^@±^G^P^]²^G^@^G^E^P^ CONNECTDLG^FOLECLI^@^@^CWEP^A^@^@^A^@^H^@^L^@^Q^@^@^FKERNEL^CGDI^DUSER +^ESHELL^A^A^C^@^@^Cÿ^CÍ?^CN^@^CÍ?^CÆ^@^CÍ?^C"^A^A^@-ÿ^CÍ?^C^@^D^Í ^Y^CÍ?^C^@^CÍ?^B> ^CÍ?^B ^CÍ?^B¶^K^CÍ?^B^L^CÍ?^BÈ^L^CÍ?^C|^\^CÍ?^Bö^L^CÍ?^C^@^]^CÍ?^CB^L^CÍ?^C4 +^P^C^@^Gÿ^CÍ?^D^@^@^CÍ?^DZ^B^CÍ?^C<^M^CÍ?^C¾^\^CÍ?^C.^U^C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M.^A^QLESETTARGETDEVICEÍ^@^PLEQ

how am i supposed to know if a byte is larger that 30(hex)...?

20060822 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips, removed H2 tags

wellll thnx for the help!!!!=) it really helped a lot

Replies are listed 'Best First'.
Re: BIN to HEX
by Roger (Parson) on Aug 22, 2006 at 07:01 UTC
    You could first open the file, then read the byte in from the file handle, then inspect the byte.

    # open a file open MYFILE, '<myfile.bin' or die 'can not open file'; # read a byte my $byte; read MYFILE, $byte, 1; # do perldoc -f read to see full doco # check the byte if ($byte > 0x30) { print "Byte is greater than 0x30\n"; } else { print "Byte is less than or equal to 0x30\n"; } # close the file close MYFILE;

      If the file is binary, you should add:

      binmode MYFILE;
      right after the open. Also the line:
      if ($byte > 0x30) {
      should read:
      if (ord($byte) > 0x30) {

      if ($byte > 0x30) {
      Shouldn't that be ord($byte) > 0x30?
Re: BIN to HEX
by AndyArmstrong (Novice) on Aug 22, 2006 at 12:48 UTC
    Firstly when people speak about converting binary into hex they're usually talking about converting a /representation/ of binary into hex. You have raw data - actually a Windows executable. For historical reasons program executables are referred to as binaries - but that doesn't mean you have to convert from binary to access them.

    Secondly reading the whole file into memory might be problematic - particularly if you may have to deal with a very large file. It's better to read the file in fixed size chunks - then you can put an upper bound on how much memory your program uses which doesn't depend on the size of the files you feed it.

    Finally unpack() is useful when working with non-textual data. Here it's used to turn a 8192 (or less) character string into an 8192 element array of byte values.

    #!/usr/bin/perl -w use strict; $| = 1; my $MAXVALUE = 0x30; my $file = shift or die "Please name a file\n"; open my $fh, '<', $file or die "Can't open $file ($!)\n"; binmode $fh; my $buffer; CHUNK: for (;;) { # Read as much as 8k, less if at end of file my $rc = sysread $fh, $buffer, 8192; die "Read error\n" unless defined $rc; # No more bytes? last CHUNK if $rc == 0; for my $byte (unpack('C*', $buffer)) { if ($byte > $MAXVALUE) { printf("%02x > %02x\n", $byte, $MAXVALUE); last CHUNK; } } } close $fh;
Re: BIN to HEX
by talexb (Chancellor) on Aug 22, 2006 at 14:49 UTC

    As a side note, the first two characters of the file tell the story: they are Mark Zbikowski's initials, and the file is a DOS executable.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: BIN to HEX
by cdarke (Prior) on Aug 22, 2006 at 07:11 UTC
    You might also like to look at the ord function, perldoc -f ord.
Re: BIN to HEX
by Fletch (Bishop) on Aug 22, 2006 at 13:30 UTC

    TMTOWTDI, some not involving Perl; see man od (or judging by the contents of your binary file after grabbing Cygwin as necessary; alternately from the PPT project, but then that would involve Perl . . .).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://568784]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-26 00:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found