Older Version
Newer Version
bshipps64
Feb 18, 2006
- "Added digit grouping code"
==DecToBin$ function==
Simple function with (very) simple demo program illustrating how to build a string containing the binary representation of a decimal number with digit grouping.
----
> Not too much explanation really necessary on this one. The function takes an integer argument and uses a DO...LOOP structure to repeatedly perform a logical AND operation on that argument and each sequential exponentiation of 2 until that exponentiation is greater than the original number. If the function is fed anything other than a nonzero integer value, it returns "0". I was fairly surprised at the speed of this function. Try entering a 50 or 60 digit number at the prompt.
[[code format="vbnet"]]
[loop]
input a$
if a$ = "" then end
print DecToBin$(val(a$))
goto [loop]
function DecToBin$(decNum)
if ((decNum <> 0) and (decNum = int(decNum))) then
decNum = abs(decNum)
do
if (decNum and 2^x) then
DecToBin$ = "1" + DecToBin$
else
DecToBin$ = "0" + DecToBin$
end if
'Comment out next line to disable digit grouping
if not((x mod 4) - 3) then DecToBin$ = " " + DecToBin$
x = x + 1
loop until (2^x > decNum)
else
DecToBin$ = "0"
end if
DecToBin$ = trim$(DecToBin$)
end function
[[code]]