Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Decimal to Binary, Octal, Hex
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
jamesonline




PostPosted: 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:

code:
%%%%%%%%%%%%%%%%%%%%%
%Binary to decimal  %
%11.30.2006         %
%James Holmes ICE3U1%
%%%%%%%%%%%%%%%%%%%%%
var binin : string
var decout : int

procedure bintodec
    decout := 0  %i must reset the value of the output each time
    put "give me a binary number; or Q to quit"
    get binin %get the binary number

    %quit if user wishes
    if binin = "Q" then
        put "Program terminated"
        quit
    end if
    put "" %spacer

    %calculate the number in decimal
    for decreasing n : length (binin) .. 1
        if binin (n) = '1' then
            decout := decout + 2 ** (length (binin) - n)
            %the output is the output plus 2 to the exponent
            %of whatever place setting that the number resides in
        end if
    end for

    %put the result
    put binin, " is ", decout
end bintodec

%run the procedure repeatedly
loop
    bintodec
end loop


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
Sponsor
Sponsor
Sponsor
sponsor
Clayton




PostPosted: Thu Nov 30, 2006 9:59 pm   Post subject: (No 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.
jamesonline




PostPosted: Fri Dec 01, 2006 9:34 am   Post subject: (No subject)

Freakman wrote:
If you could post the formulas it could help with the rest of it.


So far i know that

    - decimal to binary is dividing by 2 then using the remainder
    - binary to decimal is adding all the exponents of the placeholder (e.g. 1010 is 1*8 + 0*4 + 1*2 + 0*1 = 10)


    - decimal to octal is dividing by 8 and using the remainder
    - 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 )


    - decimal to hex is dividing by 16 and using the remainder (and 10=A, 11=B, 12=C, 13=D, 14=E, 15=F)
    - 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:
code:
var decin : int
var binin : string
var decout : int := 0
var binout : string := "null"
var octout : string := "null"
var hexout : string := "null"


I get how to do the conversion from decimal to binary:

code:
binout := intstr (decin, 0, 2)
where binout is the output, in binary and decin is the decimal value input.

the same applies for decimal to octal:
code:
octout := intstr (decin, 0, 8)


and i am having problems with decimal to hex:

code:
hexout := intstr (decin, 0, 16)


if decin = 10 then
    hexout := intstr (10, 0, 16)
elsif decin = 11 then
    hexout := intstr (11, 0, 16)
elsif decin = 12 then
    hexout := intstr (12, 0, 16)
elsif decin = 13 then
    hexout := intstr (13, 0, 16)
elsif decin = 14 then
    hexout := intstr (14, 0, 16)
elsif decin = 15 then
    hexout := intstr (15, 0, 16)
end if

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.
code:
%calculate the number in decimal
    for decreasing n : length (binin) .. 1
        if binin (n) = '1' then
            decout := decout + 2 ** (length (binin) - n)
            %the output is the output plus 2 to the exponent
            %of whatever place setting that the number resides in
        end if
    end for

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
jamesonline




PostPosted: Fri Dec 01, 2006 9:43 am   Post subject: (No subject)

i changed a few things and now i realize that the decimal to hex works ok

here is the source:
code:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%James Holmes                           %
%Decimal to Binary, Octal, Hex converter%
%12.1.06                                %
%                                       %
%Convert any integer into base 2,8 or 16%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

var decin : int
var binout : string := "null"
var octout : string := "null"
var hexout : string := "null"

procedure dec_to
put "Decimal number: "..
get decin

%solve for binary
binout := intstr (decin, 0, 2)
%solve for octal
octout := intstr (decin, 0, 8)

%solve for hex
hexout := intstr (decin, 0, 16)
if decin = 10 then
    hexout := intstr (10, 0, 16)
elsif decin = 11 then
    hexout := intstr (11, 0, 16)
elsif decin = 12 then
    hexout := intstr (12, 0, 16)
elsif decin = 13 then
    hexout := intstr (13, 0, 16)
elsif decin = 14 then
    hexout := intstr (14, 0, 16)
elsif decin = 15 then
    hexout := intstr (15, 0, 16)
end if

%output
put""
put "octal ", octout
put "binary " , binout
put "hex ", hexout

%clear all variables
binout := "null"
octout := "null"
hexout := "null"
end dec_to

%run
loop
dec_to
end loop
Alucard




PostPosted: Tue Dec 12, 2006 1:23 pm   Post subject: (No subject)

Great code, but could you explain in detail on how the

hexout := intstr (10, 0, 16)

part works? Thanks.
jamesonline




PostPosted: Tue Dec 12, 2006 4:31 pm   Post subject: (No 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:
code:
%solve for hex
hexout := intstr (decin, 0, 16)

hope that solves your problem, Alucard.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: