Author Topic: Base 10 to base x convertor  (Read 3080 times)

0 Members and 1 Guest are viewing this topic.

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Base 10 to base x convertor
« on: January 05, 2021, 01:49:45 am »
Got the idea from other post that used the MOD operator.    STxAxTIC

Ok removed some bugs.   might add features later.

Will post a base 10 to hexadecimal (base 16) convertor as well. Got the code working but want to double check it.

It turns out that it is possible to convert anywhere from base 2 to base 16 and maybe above that but base 16 is kinda standard.


Code: QB64: [Select]
  1.  
  2.  
  3. t = ""
  4.  
  5. INPUT "Enter a base 10 number"; m
  6. INPUT "Convert to what base?  2 to 9  "; b
  7. n = m
  8. IF b <= 1 OR b > 10 THEN PRINT " Can't process": END
  9.  
  10.     r = n MOD b
  11.  
  12.     t = CHR$(r + 48) + t
  13.  
  14.     n = n - r
  15.     n = n / b
  16.     IF n < b THEN
  17.         t = CHR$(n + 48) + t
  18.         EXIT DO
  19.     END IF
  20. PRINT "Base 10 number = "; m
  21. PRINT "converted to base"; b; "= "; t
  22.  
  23.  
  24.  
« Last Edit: January 05, 2021, 02:52:22 am by NOVARSEG »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Base 10 to base x convertor
« Reply #1 on: January 05, 2021, 04:15:44 pm »
Converts base 10 number to any base from 2 to 16


Code: QB64: [Select]
  1.  
  2.  
  3. t = ""
  4.  
  5. INPUT "Enter a base 10 number"; m
  6. INPUT "Convert to what base?  2 to 16 "; b
  7. n = m
  8. IF b <= 1 OR b > 17 THEN PRINT " Can't process": END
  9.  
  10.  
  11.  
  12.     r = n MOD b
  13.  
  14.  
  15.     IF r <= 9 THEN
  16.         t = CHR$(r + 48) + t
  17.     END IF
  18.     IF r > 9 THEN
  19.         t = CHR$(r + 55) + t
  20.     END IF
  21.  
  22.     n = n - r
  23.     n = n / b
  24.     IF n < b THEN
  25.  
  26.         IF n <= 9 THEN
  27.             t = CHR$(n + 48) + t
  28.         END IF
  29.         IF n > 9 THEN
  30.             t = CHR$(n + 55) + t
  31.         END IF
  32.  
  33.         EXIT DO
  34.     END IF
  35.  
  36. PRINT "Base 10 number = "; m
  37. PRINT "converted to base"; b; "= "; t
  38.  
  39.  
  40.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Base 10 to base x convertor
« Reply #2 on: January 05, 2021, 05:07:50 pm »
Have you considered extending this through decimals?

I have tried binary and drew some interesting designs when I changed 0, 1's to black and white blocks then stacked (0 to m-1) / m (rational numbers) on top of each other.

1/m  = black and white boxes like a bar code
2/m  = ...
3/m
.
.
.
m-1/m

 
image_2021-01-05_172033.png
« Last Edit: January 05, 2021, 05:20:42 pm by bplus »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Base 10 to base x convertor
« Reply #3 on: January 05, 2021, 09:52:39 pm »
Some minor bugs
 wrong variable type  - fixed

Can now convert from base 2 to base 36

Code: QB64: [Select]
  1.  
  2.  
  3. t = ""
  4.  
  5. INPUT "Enter a base 10 number"; m
  6. INPUT "Convert to what base?  2 to 36 "; b
  7. n = m
  8. IF b <= 1 OR b > 36 THEN PRINT " Can't process, base out of range": END
  9.  
  10.  
  11.  
  12.     r = n MOD b '
  13.     IF r <= 9 THEN
  14.         t = CHR$(r + 48) + t
  15.     END IF
  16.     IF r > 9 THEN
  17.         t = CHR$(r + 55) + t
  18.     END IF
  19.  
  20.     n = n - r
  21.     n = n / b
  22.     IF n < b THEN
  23.  
  24.         IF n <= 9 THEN
  25.             t = CHR$(n + 48) + t
  26.         END IF
  27.         IF n > 9 THEN
  28.             t = CHR$(n + 55) + t
  29.         END IF
  30.  
  31.         EXIT DO
  32.     END IF
  33.  
  34. PRINT "Base 10 number = "; m
  35. PRINT "converted to base"; b; "= "; t
  36.  
  37.  
  38.  
« Last Edit: January 09, 2021, 02:34:30 am by NOVARSEG »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Base 10 to base x convertor
« Reply #4 on: January 09, 2021, 01:55:14 pm »
I just re-checked this thread. Nice job folks.

bplus - have you considered putting all your stuff together in one place, or are you satisfied with writing your words in water?

You need a website and flashy github right now.
You're not done when it works, you're done when it's right.