
-----------------------------------
vg19
Sat Jan 24, 2004 5:11 pm

Hexadecimal to Decimal
-----------------------------------
Hey,

I got my decimal to binary program to work. Now I ma working on decimal to hexadecimal.

I have this so far, but no luck yet. Can someone please give me some advice?

var num : real
var result1 : real
var remainder : real

put "What is the number?"
get num
loop
result1 := num div 16
remainder := num mod 16

if result1 = 10 then
put "A"
elsif result1 = 11 then
put "B" 
elsif result1 = 12 then
put "C" 
elsif result1 = 13 then
put "D" 
elsif result1 = 14 then
put "E" 
elsif result1 =15 then
put "F" 

else 
put remainder
exit when result1 = 0
end if
end loop




-----------------------------------
McKenzie
Sat Jan 24, 2004 5:38 pm


-----------------------------------
your code de-bugged
var num : real
var remainder : real

put "What is the number?"
get num
loop
    remainder := num mod 16
    num := num div 16

    if remainder = 10 then
        put "A"
    elsif remainder = 11 then
        put "B"
    elsif remainder = 12 then
        put "C"
    elsif remainder = 13 then
        put "D"
    elsif remainder = 14 then
        put "E"
    elsif remainder = 15 then
        put "F"

    else

    put remainder
         exit when num = 0
    end if
end loop

still has a minor error. I'd use intstr, if it was not allowed use an array to encode the letters like:
var hexLett :array 10..15 of string :=init("A","B","C","D","E","F")
then you'd have
if remainder > 9 then
     put hexLett(remainder)
else
    put remainder
end if

-----------------------------------
vg19
Sat Jan 24, 2004 6:06 pm


-----------------------------------
Thanks, the only problem is that is I put it 255, it outputs FF0. A zero is outputting, and it shouldnt be there. Any way to fix this?

-----------------------------------
Delos
Sat Jan 24, 2004 8:48 pm


-----------------------------------
Ok, this one is a tad more challenging...for you...hehehe.

Ok.

Same principal as before.  div and mod the number continuously, storing the remainder.
In this case, remainders over 9 must take a string form.

If you don't know them already, this code will introduce you to loops, for loops, if statements, and case structures.  A lot?  Nope.  Just really basic stuff you need to learn really quickly.  Will make your life a lot easier.


var num : int
% The integer.
var hex : string := ""
% The resultant hex.


put "?"
get num
% Prompt for and get the number.

loop
    if num mod 16 