Convert string to integer (binary)

Posted on 2003-12-09
Last Modified: 2012-05-04
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.

Question by:dankennedy
  • 3
  • 2
LVL 24

Expert Comment

ID: 9909518
like u have a string called '123' and 123 number.

LVL 24

Expert Comment

ID: 9909538
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.

Author Comment

ID: 9909549

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...
LVL 24

Accepted Solution

shivsa earned 250 total points
ID: 9909600
try pack and unpack functions of perl.

Author Comment

ID: 9909612

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now