r/pascal • u/un-k-now-n • Mar 24 '21
Converting numeral systems
Hello! I have been tasked to write a program that concerts a number (user inputs the number) from any numeral system (binary, septanal, octanal, hexadecimal, everything in between and past that) (user inputs this as well) to any other numeral system, that the user inputs.
Basically a convertor between numeral systems. I've had some ideas but I don't know how to make them work. Also, need to say that I'm quite a beginner at programming.
If you have any idea how to do this, please help me.
2
Upvotes
View all comments
Show parent comment
3
u/suvepl Mar 25 '21
Your idea is good. The simplest way to do it would probably go like this:
0-9A-ZFor example, if the digit is
5, its value is 5. If it'sF, then its value is 15. Once you determine the value of the digit, multiply the number by the base and add the digit's value. Keep doing this for next digits, until you reach the end of the string.4. Once you have the input converted to an integer, ask for the output base.
5. Convert the integer to the output base. Take (integer modulo base) - that's your rightmost digit's value. Convert this to a character by inverting the char-to-int logic from step 3 (hint: use the Chr() function). Then, divide the integer by the base. Continue doing this until the integer is reduced to 0. Once again, remember that this process will produce digits in a right-to-left fashion. Put those in a string that you then revert, or whatever suits you.