We help IT Professionals succeed at work.

How to print a Hex Value

ted_driver
ted_driver asked
on
This site's search engine doesn't seem to be working right now......this question may have been asked before.
I want to calculate a number, an int and display it as a hexadecimal value to the screen.  How is this done?

Example:
int  test = 0XFF;
System.out.println(test); // prints 255,  I want to print FF

I would also like to be able to print the binary equivalent, can this be done also?
Comment
Watch Question

Commented:
Have a look at the static method

Integer.toHexString(int i)

which returns a String.

Cheers Tom


Commented:
The Integer class provides a method for this:

public static String toString(int i,int radix);

So you can print test in hex with:

Integer.toString(test,16);

and in binary with:

Integer.toString(test,2);

Author

Commented:
Works Great!  Just what I was looking for...
Thanks!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.