PDA

View Full Version : Change a number from any base back to base 10 and vice versa



primo
17-11-2017, 11:28
the following is an adaptation of algorithms here:
https://mathbits.com/MathBits/CompSci/Introduction/tobase10.htm
https://mathbits.com/MathBits/CompSci/Introduction/frombase10.htm
the first 2 examples is for bases from 10 to 2, for the purpose the user can compare the web pages with these examples easily. the 3rd and 4th examples from decimal to any base , and from any base to decimal.
we the humans have 10 fingers, but the Mayan people they consider the fingers and toes so their numeric system is 20.
short description of the number systems:
if the worm which have 1000 fingers wants to count then she will use symbols from (0,1,2,3,...., A,B,C,.... :cool:) and after she reach symbol :cool: ie the digit 999(base10) she will write 10 to denote one thousand and the 11 to denote one thousand and one.
to check the results in windows calculator look this example in pictures:
(for windows xp):
97669767

Base 10 to any base from 9 to 2

Uses "Console"
'https://mathbits.com/MathBits/CompSci/Introduction/frombase10.htm
Long MyNumber, base, i, num, remainder
String num2
Print "write your Number in base 10: "
MyNumber = Val(Console_ReadLine)
Print "write the Base to change your number to: "
base = Val(Console_ReadLine)
num = MyNumber

While num <> 0
remainder = Mod(num, base)
num = Int(num / base)
num2 = Str$(remainder) + num2
Wend

Print "your number in Base " + Str$(base) + " is: "
PrintL Remove$(num2, " ")

PrintL "Press a key to end program"
'---Wait for a key press
WaitKey

To Base 10 from any base (9 to 2)

Uses "Console"

Long BF,ND,I,dgt, total, indx
String MyNumber, ss
Print "BASE TO CHANGE FROM "
BF = Val(Console_ReadLine)
Print "Enter your number "
MyNumber = Console_ReadLine
ND = Len(MyNumber)

For i=ND To 1 Step -1
dgt = Val(Mid$(MyNumber, I, 1))
total = total + dgt * (BF^indx)
indx+1
Next I
Print "your number in Base 10 = "
PrintL total
WaitKey


To Base 10 from any base (use ABCDE...Z in addition to numbers)

Uses "Console"
'https://mathbits.com/MathBits/CompSci/Introduction/frombase10.htm
Long Base,ND,I,dgt, total, indx
Dim MyNumber, ss, dgtStr As String
String moreDigits = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim keyPressed As String

While keypressed <> "[ESC]" 'press esc to exit
Print "BASE TO CHANGE FROM "
Base = Val(Console_ReadLine)
Print "Enter your number "
MyNumber = Console_ReadLine
ND = Len(MyNumber)

For i=ND To 1 Step -1
dgtStr = Mid$(MyNumber, I, 1)
dgt = InStr(1,"0123456789", dgtStr)
If dgt Then
dgt = Val(Mid$(MyNumber, I, 1))
total = total + dgt * (Base^indx)
indx+1
Else
dgtStr = Ucase$(dgtStr)
dgt = InStr(1,"ABCDEFGHIJKLMNOPQRSTUVWXYZ", dgtStr)
dgt = dgt + 9
total = total + dgt * (Base^indx)
indx+1
End If

Next I
Print "your number in Base 10 = "
PrintL total
MyNumber=0: base=0: i=0: total = 0 : indx = 0
PrintL
PrintL "Press ESC key to end program, any other key to continue"
PrintL
keyPressed = Console_WaitKey

Wend




from base 10 to any base

Uses "Console"
'https://mathbits.com/MathBits/CompSci/Introduction/frombase10.htm
Long MyNumber, base, i, num, remainder
String num2, kk
String moreDigits = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim keyPressed As String
While keypressed <> "[ESC]" 'press esc to exit
Print "write your Number in base 10: "
MyNumber = Val(Console_ReadLine)
Print "write the Base to change your number to: "
base = Val(Console_ReadLine)
num = MyNumber

While num <> 0
remainder = Mod(num, base)
num = Int(num / base)
If Len(TStr$(remainder))<=1 Then
num2 = TStr$(remainder) + num2
Else
num2 = Mid$(moreDigits, remainder-9, 1) + num2
End If
Wend

Print "your number in Base " + Str$(base) + " is: "
PrintL Remove$(num2, " ")

MyNumber=0: base=0: i=0: num=0: remainder=0: num2 = ""
PrintL
PrintL "Press ESC key to end program, any other key to continue"
PrintL
keyPressed = Console_WaitKey

Wend