Qbasicnews.com

Full Version: Fastest method for converting decimal to binary..
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Take a closer peek at neo's example:

Code:
LOG(number&) / LOG(2)

this is the equivalent of Log 2 n, the solution for 2^x = n. if you say: int(log(number&) / log(2)) you get the number of digits.

so if you're using a dynamic array:
dim scraparray(200) as integer '<- oughta be enough

'do your thing

redim myarray(int(log(number&) / log(2))
then copy your array.

of course, an easier way to do it would be:

Code:
cudigit = 0
while number& <> 0
  if number& and 1 then array(curdigit) = 1 else array(curdigit = 0
  number& = number& \ 2
  curdigit = curdigit + 1
wend

and then curdigit is the number of digits used.

and to whitetiger: you could do that conversion 1000 times in your program, and some of those things I did roll over into other areas of programming, so it's a good habit to use them.

EDIT: nuts, I misunderstood your question. BRB
okay, you dont want anything to do with binary. you want to use strings. Well, that becomes a little trickier. I suppose one way is to use one of those psuedo-bigint libraries out there, but I'll think of other..
I've some code, but it need a little more testing, and only work for integers.
And isn't optimized, yet. I've to deal with very anoying overflows, and i'm not sure is completely fixed. But I'll be very busy the following weeks.

Code:
DEFINT A-Z

CONST MaxPrec = 100

DIM u(MaxPrec) AS INTEGER
DIM s AS STRING


StrToNum u(), "31415926535"
NumToStr s, u()
PRINT s

' u() + v() -> u()
SUB AddS (u() AS INTEGER, s AS INTEGER) STATIC
  DIM i AS INTEGER
  DIM c AS LONG
  c = ToLong(u(0)) + ToLong(s)
  u(0) = FromLong(c)
  c = c \ &H10000
  i = 1
  WHILE c > 0
    c = c + ToLong(u(i))
    u(i) = FromLong(c)
    c = c \ &H10000
    i = i + 1
  WEND
END SUB

' v() -> u()
SUB Copy (u() AS INTEGER, v() AS INTEGER) STATIC
  DIM i AS INTEGER
  FOR i = 0 TO MaxPrec
    u(i) = v(i)
  NEXT i
END SUB

' u() / d -> u()
SUB DivS (u() AS INTEGER, d AS INTEGER, r AS INTEGER) STATIC
  DIM i AS INTEGER
  DIM c AS LONG
  c = 0
  FOR i = MaxPrec TO 0 STEP -1
    c = c * &H10000 + ToLong(u(i))
    u(i) = FromLong(c \ d)
    c = c MOD d
  NEXT i
  'there is no overflow here
  r = c
END SUB

FUNCTION FromLong% (v AS LONG) STATIC
  DIM k AS INTEGER
  k = v AND &H7FFF&
  IF v AND &H8000& THEN
    k = k OR &H8000
  END IF
  FromLong = k
END FUNCTION

' u() * m -> u()
SUB MulS (u() AS INTEGER, m AS INTEGER) STATIC
  DIM i AS INTEGER
  DIM c AS LONG
  DIM k AS LONG
  c = 0
  FOR i = 0 TO MaxPrec
    c = c + ToLong(u(i)) * m
    u(i) = FromLong(c)
    c = c \ &H10000
  NEXT i
  ' if c<> 0 overflow
END SUB

' u() -> s
SUB NumToStr (s AS STRING, u() AS INTEGER) STATIC
  DIM v(MaxPrec) AS INTEGER
  DIM i AS INTEGER
  DIM k AS INTEGER
  DIM r AS INTEGER
  Copy v(), u()
  k = MaxPrec
  WHILE k > 0 AND v(k) = 0
    k = k - 1
  WEND
  IF k >= 0 THEN
    s = ""
    DO WHILE v(k) <> 0
      DivS v(), 10, r
      s = CHR$(48 + r) + s
      IF v(k) = 0 THEN
        k = k - 1
        IF k < 0 THEN
          EXIT DO
        END IF
      END IF
    LOOP
  ELSE
    s = "0"
  END IF
END SUB

SUB SetZero (u() AS INTEGER) STATIC
  DIM i AS INTEGER
  FOR i = 0 TO MaxPrec
    u(i) = 0
  NEXT i
END SUB

' str(s) -> u()
SUB StrToNum (u() AS INTEGER, s AS STRING) STATIC
  DIM i AS INTEGER
  DIM l AS INTEGER
  l = LEN(s)
  SetZero u()
  FOR i = 1 TO l
    MulS u(), 10
    AddS u(), VAL(MID$(s, i, 1))
  NEXT i
END SUB

FUNCTION ToLong& (u AS INTEGER) STATIC
  DIM k AS LONG
  k = CLNG(u AND &H7FFF&)
  IF u AND &H8000& THEN
    k = k OR &H8000&
  END IF
  ToLong& = k
END FUNCTION
:]
Pages: 1 2