aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes toHexString vs toString Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "toHexString vs toString" Watch "toHexString vs toString" New topic
Author

toHexString vs toString

Jerry Bustamente
Ranch Hand

Joined: May 24, 2004
Posts: 90
Can someone explain the difference between toHexString and
toString(primitive, radix) ?

For example, the following yields the same results:

public class LongToHex {

public static void main(String[] args) {

String bob = Long.toHexString(25);
System.out.println("bob = " + bob);
String ted = Long.toString(25,16);
System.out.println("ted = " + ted);
}
}

output:

bob = 19
ted = 19

Thanks alot!

:-)

JerryB
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
As far as I know, there really is no difference. In fact, I wouldn't be at all surprised if the method body of toHexString really consisted nothing more than an invocation of toString. I believe toHexString is supplied as more of a convenience method than anything.


SCJP Tipline, etc.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Jerry,

The implementation of toHexString, toOctalString and toBinaryString is simple relative to the implementation of toString(primitive, radix), because toHexString, toOctalString and toBinaryString are working with a radix that is a power of 2. The implementation of the toString(primitive, radix) method is generalized to work with any radix including those that are not a power of 2.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Jerry Bustamente
Ranch Hand

Joined: May 24, 2004
Posts: 90
Thank you Corey and Dan.

Sincerely,

:-)

JerryB
kapil munjal
Ranch Hand

Joined: May 11, 2004
Posts: 298
Thanks Corey and Dan,

you people are really helpful, I had never seen these methods in detail but now I have been able to understand it completely.

Thanks
Kaps


Kapil Munjal
SCJP 1.4, SCWCD 1.4
Boris Treukhov
Greenhorn

Joined: Dec 30, 2012
Posts: 1
A bit of necromancy(this post is showed as number one when I searched in google so I decided to post a reply).

The information above is INCORRECT,
those function differ in the way they handle negative numbers:
Long.toHexString(x) will add 2^64, so Long.toHexString(-1) will return ffffffffffffffff
Long.toString(x, 16) will use minus sign instead, so Long.toString(-1, 16) will return -1;

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: toHexString vs toString