# 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
What's the fastest way to do this..:?

input: bytestream of "0123456789"
output: integer array

input: integer array
output: bytestream of "0123456789"
Code:
```INPUT a DO a1\$ = LTRIM\$(STR\$(a MOD 2)) + a1\$ a = INT(a / 2) LOOP UNTIL a <= 0 PRINT a1\$```

remander method
Code:
```INPUT a a\$ = "" DO if a and 1 then a\$ = a\$ + "1" else a\$ = a\$ + "0" a = a \ 2 LOOP UNTIL a = 0 PRINT a\$```

a little bit faster. It's the fastest way I know anyways. I might have mixed up the 4th line though. Untested.
almost the same as mine
I use the same basic algorithm, the idea to optimize is to use as fewest string operations, (concatenate strings is an expensive job).

Code:
```input a% if a% = 0 then   a\$ = "0" else   ' in i% how many digits i need   i%=0   b% = a%   while b% > 0     i% = i% + 1     b% = b% \ 2     wend   ' i make an string with "0"s   a\$ = string\$(i%, "0")   i% = 1   while a% > 0     ' just replace the 1     if a% and 1 then         mid\$(a\$,i%,1) = "1"     endif     a% = a% \ 2   wend endif```

The code surely will not be very fast with small numbers.
I want to use integer arrays...............................................................................................................................................................................................................................
Ok, let me try:

For one way:
Code:
```number& = 6445 REDIM Array(LOG(number&) / LOG(2)) AS INTEGER FOR I = 0 TO UBOUND(Array)    IF (2& ^ I ) AND number& THEN Array(I) = 1 ELSE Array(I) = 0 NEXT I```

For the other:
Code:
```'the array is called 'array' FOR I = 0 TO UBOUND(Array)    IF Array(I) THEN nr& = nr& + (2& ^ I) NEXT I```

I hope this suits you
No.... :|
Give another example, so i can understand what are you expecting.
neo has the right idea, but he's using a long instead of a string.

Example:

"89865876578" becomes an integer array that uses as many bits as the minimum bits required to hold this string's number, 16 bits per integer. (although 15 would be OK...)

the integer array becomes "89865876578"
Pages: 1 2