Computer Science Canada Decimal to Binary, Octal, Hex |
Author: | jamesonline [ Thu Nov 30, 2006 5:34 pm ] | ||
Post subject: | Decimal to Binary, Octal, Hex | ||
Hi i am wondering if you guys could help me with some code in grade eleven ICE3M1 we need to create a turing program that can convert a decimal number to Binary, Octal, and Hexadecimal - and back! i have a handle on converting decimal to binary here is what i have so far:
i am having problems with hexadecimal conversions would it be easier to convert to binary - then to hex --or-- decimal to hex? thanks i appreciate it |
Author: | Clayton [ Thu Nov 30, 2006 9:59 pm ] |
Post subject: | |
I don't know the formulae for converting numbers from decimal to binary etc. did you get those formulae when you recieved the assignment? if not I'm surprised. If you could post the formulas it could help with the rest of it. |
Author: | jamesonline [ Fri Dec 01, 2006 9:34 am ] | ||||||||||
Post subject: | |||||||||||
Freakman wrote: If you could post the formulas it could help with the rest of it.
So far i know that
- binary to decimal is adding all the exponents of the placeholder (e.g. 1010 is 1*8 + 0*4 + 1*2 + 0*1 = 10)
- octal to decimal is adding all the products of the exponents of the placeholder (e.g. 777 is 7*64 + 7*8 + 7*1 = 511 )
- hex to decimal is adding all the products of the exponents of the placeholder (e.g. A6 is 10*16 + 6*1 = 66) here is what i have declared:
I get how to do the conversion from decimal to binary:
the same applies for decimal to octal:
and i am having problems with decimal to hex:
it keeps on getting stuck if i give it a number higher than 15 i am also having troubles with the converting them back to decimal my teacher showed me this code. it sort of works for binary to decimal, though i cannot seem to change it to work for octal or hex.
if you could please show me how to convert the hex and octal numbers back to decimal it would be awsome thank you for your help, i appreciate it greatly |
Author: | jamesonline [ Fri Dec 01, 2006 9:43 am ] | ||
Post subject: | |||
i changed a few things and now i realize that the decimal to hex works ok here is the source:
|
Author: | Alucard [ Tue Dec 12, 2006 1:23 pm ] |
Post subject: | |
Great code, but could you explain in detail on how the hexout := intstr (10, 0, 16) part works? Thanks. |
Author: | jamesonline [ Tue Dec 12, 2006 4:31 pm ] | ||
Post subject: | |||
Sorry about that... the whole if statement does not need to be there. the 'instr' command changes an integer to a string - beacuse the base 10 numbers go from 0-9 and we specify that we want it to go to base 16 (hex) the alphabet is used (A-F). Therefore the if..then statement is redundant because it has already told the program what to do. To solve this error: remove the if statement and just leave this line:
hope that solves your problem, Alucard. |