File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Logic error (regarding hexadecimal conversion) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Logic error (regarding hexadecimal conversion)" Watch "Logic error (regarding hexadecimal conversion)" New topic

Logic error (regarding hexadecimal conversion)

Philip Jenson

Joined: Jan 24, 2007
Posts: 21
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,

marc weber

Joined: Aug 31, 2004
Posts: 11343

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.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Logic error (regarding hexadecimal conversion)
Similar Threads
[Easy] Palindromes
Back to basics: if statement
How do I read (right to left) from a string one character at a time?
Stupid Question
problem with while loop