Prev: Generate a wsdl file from a cgi script
Next: FAQ 5.37 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl?
From: Uri Guttman on 1 Mar 2010 13:47 >>>>> "OA" == Oscar Almer <o.almer(a)sms.ed.ac.uk> writes: OA> I seem to recall (I wrote some code that parses binary strings OA> recently) that oct() does that, if you prefix it with "0b" to OA> indicate binaryness. Some application of reverse / substr might be OA> useful to handle byte ordering. OA> I could be wrong. you are wrong. :) she has real binary bytes. 0b deals with bits in ascii form. uri -- Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: Oscar Almer on 1 Mar 2010 13:43 On Mon, 01 Mar 2010 19:17:54 +0100 Looden <no.mail(a)spam.no> wrote: > Hi > I'm parsing a binary string, which encodes a number. > I don't know the length of the string in advance: 1, 2, 3 or 4 bytes. > The bytes of the string are in network order. > How can I retrieve the number? > > I've written this sub, there must be a better way: > > sub parse_number { > my $arg = shift; > my $size = length($arg); > my $value = 0; > my $buf; > for (my $i=($size-1);$i>=0;$i--){ > $buf = unpack "C", (substr $arg, $i, 1); > $value += $buf * (256**($size-$i-1)); > } > return $value; > } > > Note: If the length of the string was always 2 bytes, I could just > do: sub parse_n { return unpack "n", shift; } > > Thanks in advance. > I seem to recall (I wrote some code that parses binary strings recently) that oct() does that, if you prefix it with "0b" to indicate binaryness. Some application of reverse / substr might be useful to handle byte ordering. I could be wrong. //Oscar
From: Oscar Almer on 1 Mar 2010 13:53
On Mon, 01 Mar 2010 13:47:35 -0500 "Uri Guttman" <uri(a)StemSystems.com> wrote: > OA> I could be wrong. > > you are wrong. :) > > she has real binary bytes. 0b deals with bits in ascii form. > > uri > Ah, My bad. I apologize and retract the previous. //Oscar |