Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How to convert between Bases(ex. Decimal and Base64)
- shmuel3-test
- Scratcher
2 posts
How to convert between Bases(ex. Decimal and Base64)
I wasn't sure if this goes in advanced topics, or in this one but I put it here anyways. Correct me if I'm wrong.
I am was making a program that has to export a very large list(about 10,000 elements, with each a ~7 digit number) in a string that can be copy and pasted. I currently have a program that can take a number, and convert it into any base using a list of symbols. Let me elaborate.
Binary is base 2, and only has symbols, 1 and 0.
Decimal is base 10, which has 10 symbols, 0,1,2,3,4,5,6,7,8, and 9.
Hexadecimal is Base 16, which has 16 digits, 0,1,2,3,4,5,6,7,8,9, and uses letters for numbers above nine. Ten is A, Eleven is B, then C, D, E, F.
Ect, Ect, …
What I'm trying to do is implement a script that takes a reference list of what symbols represent what values, and a number and can convert to things like base 50, or 100. I can convert from Base 10 to base 100 perfectly fine, but I don't know how to convert back. Everything online just says to use the same method I used to convert, only to do it in the base of the input. This won't work because scratch can only do arithmetic in decimal. To make matters worse, the main operator in the convertion algorithm is modulo and division, the hardest thing to do in other bases(at least for me). If anyone here has experience in this, or a better way to approach this problem, I would love to hear you thoughts. Sorry for taking so much of your time, thanks for reading!
-Sam
My current code is here.
I am was making a program that has to export a very large list(about 10,000 elements, with each a ~7 digit number) in a string that can be copy and pasted. I currently have a program that can take a number, and convert it into any base using a list of symbols. Let me elaborate.
Binary is base 2, and only has symbols, 1 and 0.
Decimal is base 10, which has 10 symbols, 0,1,2,3,4,5,6,7,8, and 9.
Hexadecimal is Base 16, which has 16 digits, 0,1,2,3,4,5,6,7,8,9, and uses letters for numbers above nine. Ten is A, Eleven is B, then C, D, E, F.
Ect, Ect, …
What I'm trying to do is implement a script that takes a reference list of what symbols represent what values, and a number and can convert to things like base 50, or 100. I can convert from Base 10 to base 100 perfectly fine, but I don't know how to convert back. Everything online just says to use the same method I used to convert, only to do it in the base of the input. This won't work because scratch can only do arithmetic in decimal. To make matters worse, the main operator in the convertion algorithm is modulo and division, the hardest thing to do in other bases(at least for me). If anyone here has experience in this, or a better way to approach this problem, I would love to hear you thoughts. Sorry for taking so much of your time, thanks for reading!
-Sam
My current code is here.
- deck26
- Scratcher
1000+ posts
How to convert between Bases(ex. Decimal and Base64)
Base 100 to base 10 is simple. You'd have something like ABC as a value in base 100 which would represent A * 10000, + B * 100 + C so you just need to work through it step by step
Work out which number 0-99 is represented by A. Take the value.
Multiply by 100 and add the value represented by B.
Multiply by 100 and add the value represented by C.
In fact that's the general method, calculate what a digit represents and if it isn't the last digit multiply by the base and add the next one. Repeat.
You can also create your own mathematical optarations in the base. ABC + DE would mean adding C + E to get a new value for the units with a possible carry. Repeat until you've handled each digit and any left over carry. You may need to do this if you're working with values too large for Scratch to handle properly anyway.
Work out which number 0-99 is represented by A. Take the value.
Multiply by 100 and add the value represented by B.
Multiply by 100 and add the value represented by C.
In fact that's the general method, calculate what a digit represents and if it isn't the last digit multiply by the base and add the next one. Repeat.
You can also create your own mathematical optarations in the base. ABC + DE would mean adding C + E to get a new value for the units with a possible carry. Repeat until you've handled each digit and any left over carry. You may need to do this if you're working with values too large for Scratch to handle properly anyway.
- shmuel3-test
- Scratcher
2 posts
How to convert between Bases(ex. Decimal and Base64)
I didn't realise that! Thank you!
- deck26
- Scratcher
1000+ posts
How to convert between Bases(ex. Decimal and Base64)
That's how any base works! We're used to decimal and ABC being A*100 + B*10 + C but it's the same for any base. I didn't realise that! Thank you!
- Medericoder
- Scratcher
100+ posts
How to convert between Bases(ex. Decimal and Base64)
Hi! I made a project that convert base 2 to base 64 (and convert back)
https://scratch.mit.edu/projects/542519266/
https://scratch.mit.edu/projects/542519266/
Last edited by Medericoder (June 10, 2021 08:07:21)
- Discussion Forums
- » Help with Scripts
- » How to convert between Bases(ex. Decimal and Base64)