• 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

radix conversion

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to write a sample program that converts Decimal numbers to binary, Octal, and Hex, and vice versa and displays the results. Is there a simple way to force the output of a number to be displayed as binary, octal or Hexadecimal rather than defaulting to Decimal when used with println()?
Thanks
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

  1. println( "\ddd" ) = Octal
  2. println( "\xdd" ) = Hexadecimal
  3. println( "\udddd" ) = Unicode character

  4. Marilyn
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your just doing int's it's fairly easy....
int i = 234;
System.out.println(Integer.toBinaryString(i));
System.out.println(Integer.toHexString(i));
System.out.println(Integer.toOctalString(i));
Hope this is helpful.
 
Brian Podolny
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Carl, that's close to what I was looking for. I've already written the program I was working on and it works fine but it seems inneficient to me. I was hoping there were methods written into the language that would display numbers in Hex, bin, and octal, rather than accepting them in those forms but not displaying them. The Integer methods help but they don't cover what I'm doing. I wrote a method that accepts a String representing a number, the radix of that number, the radix it's being converted to and then outputs a String representing the number with the new base. I wanted to be able to output as an int or long rather than a String, but this doesn't seem possible, particuarly for Hex numbers.
[This message has been edited by Brian Podolny (edited September 11, 2000).]
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if you get right down to what you are seeing on the screen of your computer, it's all Strings with each character a visual representation of a numeric value (ascii on most pc's) So, writing the representation as a string isn't relivant. To convert a binary, hex or octal String to an integer value you can use
Integer.valueOf(String s, int radix)which does exactly what you are trying. The use of this and the methods earlier, should make your routine fairly easy to code.
Good Luck
 
I once met a man from Nantucket. He had 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