Ooops
January 26, 2021, 05:54:03 AM

Author Topic: [bb] Base Converter by Arem [ 1+ years ago ]  (Read 428 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Base Converter by Arem [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : Base Converter
Author : Arem
Posted : 1+ years ago

Description : Converts a number from one base to another.

Code :
Code: BlitzBasic
  1. Type character
  2.         Field symbol$,value
  3. End Type
  4.  
  5. seed$="0123456789abcdefghijklmnopqrstuvwxyz"
  6.  
  7. For a=0 To 35
  8.         c.character=New character
  9.         csymbol$=Left$(seed$,1)
  10.         cvalue=a
  11.        
  12.         seed$=Right$(seed$,Len(seed$)-1)
  13. Next
  14.  
  15. start$=Input$("Enter a number in any base: ")
  16. base=Input$("Enter the base: ")
  17. newbase=Input$("Enter the base to convert to: ")
  18.  
  19. savedstart$=start$
  20.  
  21. If Not base<37 And newbase<37
  22.         Print "Base must be less than 37!"
  23.         Delay(5000)
  24.         End
  25. End If
  26.  
  27. total=0
  28. power=0
  29.  
  30. While Not start$=""
  31.         currentsymbol$=Right$(start$,1)
  32.        
  33.         For c.character=Each character
  34.                 If csymbol$=Lower$(currentsymbol$)
  35.                         currentnumber=cvalue
  36.                 End If
  37.         Next
  38.        
  39.         If currentnumber>base-1
  40.                 invalid=1
  41.         End If
  42.        
  43.         total=total+currentnumber*base^power
  44.        
  45.         start$=Left$(start$,Len(start$)-1)
  46.         power=power+1
  47. Wend
  48.  
  49. While Not total=0
  50.         currentnumber=total Mod newbase
  51.        
  52.         total=total-currentnumber
  53.         total=total/newbase
  54.        
  55.         For c.character=Each character
  56.                 If cvalue=currentnumber
  57.                         currentsymbol$=csymbol$
  58.                 End If
  59.         Next
  60.        
  61.         output$=currentsymbol$+output$
  62. Wend
  63.  
  64. If invalid
  65.         Print savedstart$+" is not a valid base "+base+" number!"
  66. Else
  67.         Print output$
  68. End If
  69.  
  70. Delay(5000)


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal