Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Logic error (regarding hexadecimal conversion)

 
Philip Jenson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone.
This is the second time I've came here with a specific question. Everyone was great last time - I learned so much in the process. This time, my question is quite simple.

Basically, I am inputting an integer. The integer needs to be transformed into a "hexcode." In this example, 525 is input. It needs to be turned into 0D (525 % 16 % 16). However, it is output as DD. No matter what number I put in, the first and second digits are the same.

This is the process I am going for:
An integer % 16 -> Result (we'll call it code1) is sent to toHexDigit, where appropriate digit is returned (hex1).
Now, code1 % 16. This result (code2) is sent to toHexDigit, where appropriate digit is returned (hex2).
After that, I want to assign both of the returned digits (hex1 % hex2) to a variable, called code.

I've written the whole process out on paper and debugged it several times. I'm missing something, apparently. It seems that the integrity of one of the values is lost at some point. If anyone has any tips, please let me know. I've been staring at this for a long time with no improvement.

Thanks a million,
Philip






 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Philip Jenson:
...No matter what number I put in, the first and second digits are the same...

Consider the line:

int code2 = code1 % 16;

At this point, we know that code1 is less than 16, because it was set to the remainder of num/16. So code1 will always equal code1.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic