#81842 - 2003-09-09 05:59 PM
DecToBin() and BinToDec()
|
Radimus
Moderator
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
we already have the dectohex version... the binary versions would be handy.
especially for things like: IP comparisons and subnet matching
|
Top
|
|
|
|
#81843 - 2003-09-10 09:44 AM
Re: DecToBin() and BinToDec()
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Why limit it to a particular base?
code:
$iNewValue=BaseConvert($iOldValue,CurrentBase,RequiredBase, Optional WordSize)
The Optional WordSize would be required to deal with negative numbers and overflows.
|
Top
|
|
|
|
#81846 - 2003-09-15 02:14 PM
Re: DecToBin() and BinToDec()
|
Kdyer
KiX Supporter
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
quote:
How well does it handle negative values?
Probably needs to have an ABS() thrown in there before conversion..
Kent
|
Top
|
|
|
|
#81847 - 2003-09-15 03:52 PM
Re: DecToBin() and BinToDec()
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Heh, no, that actually ignores the issue
What I'm talking about is (for example) binary representation of a negative number. Normally twos-complement - I don't think ones-complement is much used.
For example the offset for time zones is sometimes negative, which is stored in the registry as a twos-complement value.
That's why in my recommendation there is an optional word size - you need to know how wide the target is to construct a twos-complement value.
|
Top
|
|
|
|
Moderator: Lonkero, ShaneEP, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart
|
0 registered
and 370 anonymous users online.
|
|
|