Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic


by drip (Beadle)
on Aug 22, 2006 at 06:50 UTC ( #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 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?^C^ ^@^@^C? ^D^C? 8^D^C?^CPG^C? ^@^C? .^A^C? d^A^A^@^D^C?^C`K^C?^N^E^C? ^D^C? "^BY^@^R^C?^M4^@^C?^M^A^C?^M^@^C?^M^@^C?^M^@^C?^Mh^@^C?^M^@^ +C?^M^D^A^C?^M8^A^C?^MR^A^C?^Ml^A^C?^M"^B^C?^M^A^C?^M z^C^@?^B^B^@?^CXB^@?^B^G^@?^B ^F^@?^Br^N^@?^C@^@?^C7^@?^CR&^@?^DpI^@?^C^_^@?^D@L^@?^P^A^@ +?^D.^@?^D.^@?^C.^@?^C^0^@?^CH^@?^C(^@?^C:M^@?^P^B ^@?^C^H<^@?^D^]^@?^D^@$^@?^D^F^@?^C^_^@?^C^_^@?^D^B^@?^D +<^@?^DD^^^@?^D^H^@?^D^H^@?^CJ'^@?^C=^@?^D^Z^C^@?^H^Z^C ^B^@?^N*^C^@?^P^@^@^@?^CNV^@?^B^^E^@? ^\^F^@? ^G^@ +?^M<^B^@?^M^@^@^@?^M^@^@?^M^Z^@^@? ^^^D^@?^C8^@?^CW^@?^CTW^@?^CRQ^@?^B^@^@^@^@LOLE Client. support + (c) Copyright Microsoft Corp. 1990 - All Rights Reserved^@^@^NGEN ERREXECUTE^B^HERRCLOSE^B MFRELEASE,^A^KLEQUERYOPEN^@^QOLEQUERY +OUTOFDATE^_^@^NLESAVETOSTREAM^@ ERRSETDATA^B BMRELEASE^A^ROLECREATEINVISIBLE:^@ LESETDATA^@^NM +FSAVETOSTREAM.^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 (Canon) 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 (Chancellor) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://568784]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2018-07-21 08:17 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (445 votes). Check out past polls.