Convert string to integer (binary)

I have a 4 byte string which I would like to convert to a integer. I'm reading the data from a file and the file contains 4 bytes for the ID number. So I've extracted the 4 bytes as text into a variable, but I want to print it's value as an integer.

And I don't only want to print it, I'd like to actually have a variable which contains the data as an integer, so that I can work with it.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

like u have a string called '123' and 123 number.

like u have $string called '1234'. u want it to convert $int which is a int.


$int = 0+ $string
print "$int \n";
and now u can work with this variable $int which is an integer.
dankennedyAuthor Commented:

I'm reading 4 bytes from a file.

Binary: 00000000 00000000 00000000 01000000

So when I read it into Perl, it's represented as a string...

String: '\0\0\0@'

But I want it to be represented as an integer..

Integer: 64

So I need code that will take a 4 byte string and give me it's integer value...
try pack and unpack functions of perl.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dankennedyAuthor Commented:

Here's how it works for anyone else who's wondering...

use the function unpack.

$myint = unpack("N",$mystring);

or for little endian...

$myint = unpack("V",$mystring);
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.