Results 1 to 1 of 1

Thread: Change a number from any base back to base 10 and vice versa

  1. #1

    Change a number from any base back to base 10 and vice versa

    the following is an adaptation of algorithms here:
    https://mathbits.com/MathBits/CompSc...n/tobase10.htm
    https://mathbits.com/MathBits/CompSc...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,.... ) and after she reach symbol 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):
    dec.JPGbin.JPG

    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
    
    Last edited by primo; 17-11-2017 at 16:20.

Similar Threads

  1. Accessing base type methods: How + example
    By Petr Schreiber in forum Sources, Templates, Code Snippets, Tips and Tricks, Do you know ...
    Replies: 0
    Last Post: 13-06-2015, 11:28
  2. Moon Base?
    By Charles Pegge in forum Shout Box Area
    Replies: 1
    Last Post: 19-03-2012, 11:28
  3. Discussion: Time base for TBEM
    By ErosOlmi in forum TBEM module - thinBasic Event Manager module
    Replies: 12
    Last Post: 10-10-2008, 20:22
  4. AMD: change all
    By ErosOlmi in forum Technology
    Replies: 0
    Last Post: 07-10-2008, 20:55
  5. base conversion
    By Michael Clease in forum Sources, Templates, Code Snippets, Tips and Tricks, Do you know ...
    Replies: 3
    Last Post: 11-11-2007, 23:00

Members who have read this thread: 1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •