Author Topic: Binary Strings  (Read 2712 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Binary Strings
« on: March 29, 2013, 05:30:20 AM »
Supports digit padding

function binarystr(sys ii, optional nd) as string
if ii=0 and nd=0 then return "0"
sys b,j,p
string bi
b=1
n=nd
if n=0 then n=8*sizeof sys
p=n
bi=space n
for j=n to 1 step -1
  if ii and b
    mid bi,j,"1"
    p=j
  else
    mid bi,j,"0"
  end if
  shl b,1
next
if nd=0
  return mid bi,p 'trim leading zeros
else
  return bi
end if
end function


print binaryStr 42
print binaryStr 42,8