Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Convert string to integer (binary)

Posted on 2003-12-09
5
Medium Priority
?
71,372 Views
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.

Thanks!
0
Comment
Question by:dankennedy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 24

Expert Comment

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

$string=sprintf"%03d",123;
$integer=0+$string;
0
 
LVL 24

Expert Comment

by:shivsa
ID: 9909538
like u have $string called '1234'. u want it to convert $int which is a int.

then

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

Author Comment

by:dankennedy
ID: 9909549
No...

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

Accepted Solution

by:
shivsa earned 1000 total points
ID: 9909600
try pack and unpack functions of perl.
0
 
LVL 2

Author Comment

by:dankennedy
ID: 9909612
Thanks.

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

715 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