# extract number from binary string

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

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; }

## Re: extract number from binary string

L> I'm parsing a binary string, which encodes a number.  I don't know
L> the length of the string in advance: 1, 2, 3 or 4 bytes.  The bytes
L> of the string are in network order.  How can I retrieve the number?

L> sub parse_number {
L>                 my \$arg = shift;

pick a better name for that. \$arg is generic.

L>                 my \$size = length(\$arg);
L>                 my \$value = 0;
L>                 my \$buf;
L>                 for (my \$i=(\$size-1);\$i>=0;\$i--){
L>                         \$buf = unpack "C", (substr \$arg, \$i, 1);
L>                         \$value += \$buf * (256**(\$size-\$i-1));
L>                 }
L>                 return \$value;
L>         }

much simpler would be to unpack the bytes to an array and then do the
multiply/add stuff in a loop. untested and unfinished:

my @bytes = unpack 'C*, \$byte_string ;

my \$value ;
for my \$byte ( @bytes ) {

}

uri

--
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

## Re: extract number from binary string

Looden wrote:

\$ perl -le'
my @strings = ( "\xa", "\xb\x1", "\xc\x2\x3", "\xd\x4\x5\x6" );
for my \$string ( @strings ) {
my \$number = substr "" . \$string, -4;
print unpack "N", \$number;
}
'
10
2817
786947
218367238

John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway

## Re: extract number from binary string

On Mon, 01 Mar 2010 19:17:54 +0100

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

## Re: extract number from binary string

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@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

## Re: extract number from binary string

On Mon, 01 Mar 2010 13:47:35 -0500

I apologize and retract the previous.

//Oscar

## Re: extract number from binary string

That is correct, but the OP does not actually have binary strings, but
rather octet strings.

If the problem was as stated in the subject then there would be no issue
of byte ordering, at least not on the input side.

--
Shmuel (Seymour J.) Metz, SysProg and JOAT  <http://patriot.net/~shmuel

Unsolicited bulk E-mail subject to legal action.  I reserve the
right to publicly post or ridicule any abusive E-mail.  Reply to
domain Patriot dot net user shmuel+news to contact me.  Do not

## Re: extract number from binary string

I guess you could also do:

sub parse_number {
my \$arg = shift;
vec(""x(4-length \$arg).\$arg, 0, 32);
}

-sln

## Re: extract number from binary string

No, you're parsing a number, not a binary string. More precisely, you're
parsing an octet string.

You can use interpolation in the template of pack, or explicit
concatenation. What do you get with

\$value = pack "N\$size", unpack 'C', \$arg

?

--
Shmuel (Seymour J.) Metz, SysProg and JOAT  <http://patriot.net/~shmuel

Unsolicited bulk E-mail subject to legal action.  I reserve the
right to publicly post or ridicule any abusive E-mail.  Reply to
domain Patriot dot net user shmuel+news to contact me.  Do not