Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Logic error (regarding hexadecimal conversion)

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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






 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic