 Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki Blog Search Turing Chat Room Members       Author Message
vg19 Posted: Sat Jan 24, 2004 5:11 pm   Post subject: 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?

 code: 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  Posted: Sat Jan 24, 2004 5:38 pm   Post subject: (No subject)

 code: 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:
 code: var hexLett :array 10..15 of string :=init("A","B","C","D","E","F")

then you'd have
 code: if remainder > 9 then      put hexLett(remainder) else     put remainder end if vg19 Posted: Sat Jan 24, 2004 6:06 pm   Post subject: (No subject)

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  Posted: Sat Jan 24, 2004 8:48 pm   Post subject: (No subject)

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.

 code: 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 <= 9 then         hex := hex + intstr (num mod 16)         % If the remainder is btwn 0 and 9, no problem.         % NOTE:         % intstr converts an integer to a string.         % This is called type conversion.     else         case num mod 16 of                 % Case structures.  Similar to if's.                 % Only ever useful sometimes.                 % I could have done this w/ if's, but used this for                 % demonstration.             label 10 :                 hex := hex + "A"             label 11 :                 hex := hex + "B"             label 12 :                 hex := hex + "C"             label 13 :                 hex := hex + "D"             label 14 :                 hex := hex + "E"             label 15 :                 hex := hex + "F"         end case     end if     num := num div 16     % Go to the next number...     exit when num = 0 end loop for decreasing i : length (hex) .. 1     % For loops.  AKA Counted loops.     % This is decreasing, so it goes from a big number to a small number.     % NOTE:  length give the number of characters in a string.     put hex (i) .. end for

You can thank me for the commenting later.
For now, look at the structures, and PLAY AROUND WITH THEM. That is the best way to learn. Just open your interpreter, and punch in some code. Errors will pop up, and you'll teach yourself how to avoid them etc.

Basic Edward Lee Thorndike stuff there. And if you don't know what I'm talking about...pick up a basic Psychology book...hehhe... vg19 Posted: Sun Jan 25, 2004 2:53 pm   Post subject: (No subject)

Hey,

Here is my program. I almost got it. The only prob is that the result is inverted. Example 111 should be 6f not f6. I tried changing it by putting (2)(1) but its not working. Can someone please help?

 code: var num : int var remainder : string put "What is the number?" get num loop     remainder := intstr (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(2)(1)..              exit when num = 0     end if end loop Delos  Posted: Sun Jan 25, 2004 3:00 pm   Post subject: (No subject)

Look at the code above^.

What you need to do is to store the result of each computation in a string. Then display the string backwards.

So, for each mod, add the result to your string. Then, at the end use a for loop to display the string backwards.

Or, if you know the length of the string before hand, then you can manually display this. Problems will arise here if the specified substring does not exist - i.e. you try to display the 8th character in a 2-character string.

Best bet: check out the code^. If you have any questions surrounding how it was done/what the commands mean, post here and I (or someone else) shall be around to answer. vg19 Posted: Sun Jan 25, 2004 3:10 pm   Post subject: (No subject)

Hi,

thanks for the adivce. something else i was think was that I could "trick" turing. I only need this to work upto 256. Could I add a space to the end of each string. If the max decimal number is 256, then the output is only goning to be 2 characters. If I add a space to the end, and then tell turing to output characters 2 and 1, it should always work, right? Delos  Posted: Sun Jan 25, 2004 3:51 pm   Post subject: (No subject)

Expand...

As in having a string with a default length, then adding accordingly? (Think a car dash-board distance tracking thingy).

So, 1 in hex would be 00000001.
And 10 would be 0000000A.

Sure!

Since you are working with strings, you can 'trick' Turing if you like. The technical term is 'hardwiring' [sp?].

For this, you need to know the repeat function. This is used w/ strings to repeat a series of characters. I'm sure that by know you know the length function that finds the length of a string. Amazing how complicated the commands/predefs in Turing are!

Ok, here's repeat in action:

You have a number with a possible 8 digits. You always want all 8 digits showing no matter what.
So...you do this:
 code: var number : string % In string so you can use repeat. get number number := repeat ("0", 8-length(number)) + number put number

The 4th line creates a pad of 0's. The number of digits in that pad is determined by "8-length(number)". i.e., with a maximum of 8 digits, how many digits are already filled, and how many more do I need to fill the remaining?

So, there you go. PLAY AROUND with it and you'll figure out how to use that for the hex thing.

Have fun.    vg19 Posted: Sun Jan 25, 2004 4:24 pm   Post subject: (No subject)

Hi,

Actually thats not really what I meant. Hopefully this code will explain what I meant. Everything works beautifully now except for the fact that anthing above 15 comes inverted. example, 111 should be 6f not f6. PLEASE please help me fix this problem

 code: var num : int var remainder : string put "What is the number?" get num loop     remainder := intstr (num mod 16)         if length (remainder) = 1 then        put remainder                             elsif remainder = "10" or num = 10 then             put "A" ..         elsif remainder = "11" or num =11 then             put "B" ..         elsif remainder = "12" or num = 12 then             put "C" ..         elsif remainder = "13" or num = 13 then             put "D" ..         elsif remainder = "14" or num = 14 then             put "E" ..         elsif remainder = "15" or num = 15 then             put "F" ..                      elsif length(remainder) = 2 then         put remainder(2)..         put remainder(1)               end if       num := num div 16     exit when num = 0 end loop Delos  Posted: Sun Jan 25, 2004 4:42 pm   Post subject: (No subject)

And I'll say it again.

You are at the moment directly outputting the remainders to the screen. Though this works fine for numbers 1-F, it screws up after that.
1, in reverse is 1.
F, in reverse is F.
FE, in reverse is EF.

Using direct display complicates things unnecassarily. You need to be able to take the remainder, and add it onto the variable each time you mod the number.
So, you'll need something like:
 code: remainder := remainder + intstr(num mod 16)

Once you've done that, then you can do the whole "if length(...) = 1 / elsif length(...) = 2" thing and it will be fine.

So, in conclusion: catenation is the word of the day. McKenzie  Posted: Sun Jan 25, 2004 8:00 pm   Post subject: (No subject)

You never got back to me on the intstr, if it's allowed then:
 code: var num : int put "What is the number?" get num put "In hex :", intstr(num,0,16) vg19 Posted: Sun Jan 25, 2004 8:35 pm   Post subject: (No subject)

lol, we have to develop our own code. I asked my teacher about using instr....no good Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First        Page 1 of 1  [ 12 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: