• 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

help: java program to convert decimal number to hexadecimal number

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all, I have been trying to write a program to convert from decimal to hexadecimal. My program works well except that for inputs =and < 16 the output is always 1. Also there is no output for decimal numbers 32, 48, 64 etc which correspond to 20, 30, 40 etc. I will gladly welcome any help.


[Edit - formatted and added code tags - see UseCodeTags for details]
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unrelated to the problem you're facing, but if this code is not intended for learning puposes I would strongly suggest using the Integer#toHexString() method instead.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gamua Tachu wrote:My program works well except that for inputs =and < 16 the output is always 1. Also there is no output for decimal numbers 32, 48, 64 etc which correspond to 20, 30, 40 etc.


I copied your code and tried it out. First of all, it doesn't compile because there's something strange at the end of line 21. After fixing that, I get different results than what you describe here.

For example, for 5 I get "0 5". For 10 I get "1". For 16 I get "1 0". For 32 I get "2 0". The results for 5, 16 and 32 look correct, except that you print a space between the digits. Only the "1" when you enter 10 looks wrong.

Follow in your head what the code does when you would enter 10, or run the program in a debugger, enter 10 and go through the code step by step to find out what happens. You'll discover that it does something else then what you had thought it would do.

The code also looks a bit complicated. There are much simpler and shorter ways to convert a number to a hexadecimal string.
 
Gamua Tachu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Mr jong: Thanks very much for taking time out to run my program. I have also discovered a shorter and more effective way of converting. My program is truely complex. The new program is simple and can easily be understood. Thanks also for proposing to me to use a debugger. I never knew of anything like that. I greatly appreciate your advice. With people like you, my solo journey of learning java will be enjoyable.

@Mr klap: Thanks very much also for your contribution. This program I wrote was just for learning purpose. I am trying to build my programming skills. Thanks very much!!
 
What's brown and sticky? ... a stick. Or a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic