Discussion:
base_convert bug?
Radek Krejča
2014-07-28 09:43:29 UTC
Permalink
Hello,

function base convert from base(16) to base(2) returns wrong value whenever the binary string starts with zeros
it eliminates all 0s in the left of the string until first (1)

example :
base_convert("10",16,2);

//return value 10000
//correct value 00010000

Is it bug? Thank you.

Radek
Ashley Sheridan
2014-07-28 10:13:30 UTC
Permalink
Post by Radek Krejča
Hello,
function base convert from base(16) to base(2) returns wrong value
whenever the binary string starts with zeros
it eliminates all 0s in the left of the string until first (1)
base_convert("10",16,2);
//return value 10000
//correct value 00010000
Is it bug? Thank you.
Radek
That's not a bug, they're both the same number. Leading zeros are always ignored in number systems, and PHP doesn't attempt to format it assuming and specific word size. Try the same conversion in your computers calculator.

To format the number as you wish for output, use printf or sprintf to pad with leading zeros.
Thanks,
Ash
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Radek Krejča
2014-07-28 11:51:33 UTC
Permalink
Post by Ashley Sheridan
That's not a bug, they're both the same number. Leading zeros are
always ignored in number systems, and PHP doesn't attempt to format it
assuming and specific word size. Try the same conversion in your
computers calculator.
To format the number as you wish for output, use printf or sprintf to
pad with leading zeros.
[Radek Krejca]
Thank you for response, but maybe it is not important when we talk about Decimal numbers , but with binary i think it is important to keep zeros ,because the position of bit in the string is very
important , to construct its value , or in Decoding ,Encoding process
Aziz Saleh
2014-07-28 11:56:37 UTC
Permalink
Post by Radek Krejča
Post by Ashley Sheridan
That's not a bug, they're both the same number. Leading zeros are
always ignored in number systems, and PHP doesn't attempt to format it
assuming and specific word size. Try the same conversion in your
computers calculator.
To format the number as you wish for output, use printf or sprintf to
pad with leading zeros.
[Radek Krejca]
Thank you for response, but maybe it is not important when we talk about
Decimal numbers , but with binary i think it is important to keep zeros
,because the position of bit in the string is very
important , to construct its value , or in Decoding ,Encoding process , it
will be also very important .
Thank you
Radek
Leading zeros are not important in a binary number. Any system can detect
that 00010000 == 10000. The leading zeros do not make a difference when it
comes to calculating the values.
Radek Krejča
2014-07-28 12:59:25 UTC
Permalink
Leading zeros are not important in a binary number. Any system can detect that 00010000 == 10000. The leading zeros do not make a difference when it comes to calculating the values.

[Radek Krejca]
Thank you. So I have to
Ashley Sheridan
2014-07-28 13:21:39 UTC
Permalink
Post by Aziz Saleh
Leading zeros are not important in a binary number. Any system can
detect that 00010000 == 10000. The leading zeros do not make a
difference when it comes to calculating the values.
[Radek Krejca]
Thank you. So I have to fill missing zeros by myself.
Radek
Yes, you can use printf or sprintf to do this. The manual lists ways of adding leading characters to a string (as this is what it essentially needs to be to preserve the leading zeros)

Thanks,
Ash
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Robert Cummings
2014-07-28 15:01:54 UTC
Permalink
Post by Ashley Sheridan
Post by Aziz Saleh
Leading zeros are not important in a binary number. Any system can
detect that 00010000 == 10000. The leading zeros do not make a
difference when it comes to calculating the values.
[Radek Krejca]
Thank you. So I have to fill missing zeros by myself.
Radek
Yes, you can use printf or sprintf to do this. The manual lists ways of adding leading characters to a string (as this is what it essentially needs to be to preserve the leading zeros)
Might I suggest the str_pad() function whose entire purpose is to pad :)

Cheers,
Rob.
--
Phone: 613-822-9060 +++ Cell: 613-600-2836
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ashley Sheridan
2014-07-28 17:26:17 UTC
Permalink
Post by Ashley Sheridan
On 28 July 2014 13:59:25 BST, "Radek Krejča"
Post by Aziz Saleh
Leading zeros are not important in a binary number. Any system can
detect that 00010000 == 10000. The leading zeros do not make a
difference when it comes to calculating the values.
[Radek Krejca]
Thank you. So I have to fill missing zeros by myself.
Radek
Yes, you can use printf or sprintf to do this. The manual lists ways
of adding leading characters to a string (as this is what it
essentially needs to be to preserve the leading zeros)
Might I suggest the str_pad() function whose entire purpose is to pad :)
Cheers,
Rob.
Good call, sprintf is probably akin to taking a mallet (not a full sledgehammer, that's more like preg_replace() ) to a walnut!

Thanks,
Ash
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...