DelphiFAQ Home Search:

How do I convert bits into ints?


commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.


How can I convert a number from binary format (0 and 1) to decimal?


You need to use pack() the data to an eight bit binary structure then unpack it to the desired target structure with unpack() - or if the target is decimal, you can use ord().

A few words on pack(). This function is very useful when processing CGI posted data, since this is often escaped. The first argument in pack() is a template. There are quite a few values - for a full documentation look here:

# convert from binary to decimal
$decimal = unpack('c', pack('B8', '10110110'));  

# same functionality but using ord()
$decimal = ord(pack('B8', '10110110'));

# from hex to binary
$binary_string = unpack('B', "\x41");  

# unescaping CGI data
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($value))/eg;


2012-11-12, 02:07:19
anonymous from United Kingdom  
2012-11-21, 03:22:31



NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: