Go to notes and algorithms index. ~ Go to home page.

Convert from a Binary Number to a Large Number Base (and Back Again).


This program converts a binary number to an ASCII representaion of the number in your base.

This program is a demonstration of how you can convert an ordinary integer number to one in a larger number base. The program is currently set-up to convert a number to base 43 as an example, but you can easily change the numberbase% variable from 43 to another number base and then in the the following numchar$ array define the charaters that represent every value of a single digit. For example you could change numberbase% to 16 and comment-out numchar$(16) upwards for the more usual binary to hex conversion.

Currently the numchars$ array is set-up in in a logical way but there is no reason why you should start with digits 0 to 9 then run through the alphabet, you can use any type-able charater in any order, but make sure you don't have any duplicates.

You'll notice in the numchar$ array that letters 'I' and 'O' are omitted... this is to avoid them being confused with zero and one when people read or type back in the number. You could amend the part of the program, that converts a large based number back into binary, so that if the letter O was typed-in it would automatically be converted to zero and the letter I to one.

This program is written in MS Quick BASIC, but I expect it will convert to MS Visual BASIC very easily.... and it would not be a mamoth task to convert it to a couple of nice C routines one to convert to a base and the other one to convert a number back again to binary.

An example use of this type of conversion is: if you have a game where you display a code on screen for the player to write down (it may be a number that represents their score and co-ordinates) that number may be quite long in decimal and a bit of a bind for the player to remember or write down accurately... but if you convert it to a larger number base then the number may be a couple of digits shorter and look more 'interesting' and memorable.

This program would need adapting to work with floating point numbers... anyway it would usually be easier to convert your floating point number to an integer by some fixed, and reversable, algorithm first and then feed it into the base conversion algorithm.



' Convert to and from any number base.
' Set the number base in numberbase% and define a character for all the
' digits in the array numchars$.
'
' Jon P, Feb 2005


numberbase% = 43
DIM numchars$(numberbase%)
numchars$(0) = "0"
numchars$(1) = "1"
numchars$(2) = "2"
numchars$(3) = "3"
numchars$(4) = "4"
numchars$(5) = "5"
numchars$(6) = "6"
numchars$(7) = "7"
numchars$(8) = "8"
numchars$(9) = "9"
numchars$(10) = "A"
numchars$(11) = "B"
numchars$(12) = "C"
numchars$(13) = "D"
numchars$(14) = "E"
numchars$(15) = "F"
numchars$(16) = "G"
numchars$(17) = "H"
numchars$(18) = "J"
numchars$(19) = "K"
numchars$(20) = "L"
numchars$(21) = "M"
numchars$(22) = "N"
numchars$(23) = "P"
numchars$(24) = "Q"
numchars$(25) = "R"
numchars$(26) = "S"
numchars$(27) = "T"
numchars$(28) = "U"
numchars$(29) = "V"
numchars$(30) = "W"
numchars$(31) = "X"
numchars$(32) = "Y"
numchars$(33) = "Z"
numchars$(34) = "*"
numchars$(35) = "&"
numchars$(36) = "%"
numchars$(37) = "$"
numchars$(38) = "="
numchars$(39) = "#"
numchars$(40) = "@"
numchars$(41) = "?"
numchars$(42) = "-"

CLS
'
' Convert from decimal to base n....
'
INPUT "Enter number to convert to base n ", num&
PRINT "Original number in decimal ="; num&
numberstr$ = ""
WHILE num&
   digitnum% = num& MOD numberbase%
   numberstr$ = numchars$(digitnum%) + numberstr$
   num& = num& \ numberbase%  '** Make sure you use the backslash here or make sure the divide operator you use does not do any rounding up.
   
WEND

PRINT "Number converted to base"; numberbase%; "= "; numberstr$

'
' Convert from a string containing a number in base n to decimal...
'
tothepowerof% = 0
num& = 0
FOR i% = LEN(numberstr$) TO 1 STEP -1
   char$ = MID$(numberstr$, i%, 1)

   ' find where in the array the digit character was...
   digit% = -1
   FOR j% = 0 TO numberbase% - 1
      IF numchars$(j%) = char$ THEN digit% = j%
   NEXT j%
   IF digit% < 0 THEN PRINT " Error digit not found."

   num& = num& + (digit% * (numberbase% ^ tothepowerof%))
   tothepowerof% = tothepowerof% + 1
NEXT i%

PRINT "The number converted back to decimal ="; num&