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

Binary to decimal conversion

by andreas1234567 (Vicar)
on Dec 11, 2007 at 08:30 UTC ( #656344=perlquestion: print w/replies, xml ) Need Help??
andreas1234567 has asked for the wisdom of the Perl Monks concerning the following question:


I have difficulties converting data on a binary format to decimal numbers related to this node. I suspect the number are encoded using a float. I'm able to see what the results should be using a third-party tool.

The number should perform as follows:

  • 3F F0 00 00 00 00 00 00 should yield 1.
  • 40 59 00 00 00 00 00 00 should yield 100.
  • 41 33 1A 9D 00 00 00 00 should yield 1251997.
What kind of conversion of unpack trickery am I in search for?

A hexdump of the source file is available on my pad (relax it's only 192 bytes).

Update: It seems like this does the trick (updated as shown by Anonymous Monk):

# read number: 8 bytes, return decimal value sub getnumber { my $buffer = undef; my $num = read($FH, $buffer, 8); return unpack 'd*', reverse $buffer; }
Update: pfaut++ and Fletch++ for good advice to stubborn monk.

Replies are listed 'Best First'.
Re: Binary to decimal conversion
by Anonymous Monk on Dec 11, 2007 at 10:19 UTC
    Too much joining :)
    return unpack 'd*', reverse $buffer;
      Hmm.. I'm surprised to see it works to reverse a scalar like that. read's 2nd argument is a SCALAR, and reverse's single argument is a LIST. What is it I do not understand?

        From reverse

        reverse LIST
              In list context, returns a list value consisting of the ele-
              ments of LIST in the opposite order.  In scalar context, con-
              catenates the elements of LIST and returns a string value with
              all characters in the opposite order.
        90% of every Perl application is already written.
Re: Binary to decimal conversion
by Anonymous Monk on Dec 11, 2007 at 09:24 UTC
    // read number function readNumber(){ $num = fread($this->buffer, 8); $r = ""; for($byte = 7 ; $byte >= 0 ; $byte--) { // reverse the bytes $r .= $num[$byte]; } $ret = unpack("dnum",$r); return $ret['num']; }
      That looks at lot like the lines 153-162 from here (direct link here):
      153 // read number 154 function readNumber(){ 155 $num = fread($this->buffer, 8); 156 $r = ""; 157 for($byte = 7 ; $byte >= 0 ; $byte--) { // reverse the bytes 158 $r .= $num[$byte]; 159 } 160 $ret = unpack("dnum",$r); 161 return $ret['num']; 162 }
      And I believe that's PHP, not Perl. I'm thankful for the suggestion although still looking for the Perl equivalent of the above.
        Translate that to perl, its easy

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://656344]
Approved by almut
[marto]: good morning all
[Corion]: Hi marto!
[Corion]: The fun show at $work continues, as The Big Project is now in its second week of frantic live-bugfixing and weekend releases where nobody knows what went live. Nothing has been tested anyway.
erix mutters cantankerously under his breath
Corion watches from the sidelines. Or rather, from behind, as my system only gets output from that process and my programs adhere strictly to the GIGO design principle.
[erix]: ah, that's nice to hear Corion :)
[Corion]: erix: Yeah, the sad thing is that all I can do is document things, so I can point fingers when the auditors come :-/
[Corion]: "I'm here to open tickets and point fingers. And I'm all out of tickets."
[erix]: didn't Sybase have pretty good auditing? :) (this is a vague memory)
[erix]: (culprits often are upstream of db of course)

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2017-03-28 08:57 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (328 votes). Check out past polls.