• 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

converting float hex to int

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,
I have a strange stiuation here.
I need to use a float number(because it is 20 or more, digits long), then calculate hexadecimal of this number, and then save the result in an integer and use that as a key.

However, the below program prints 0x1.5af1d8p66

float x= 99999999999999999999f; //20 9's
System.out.println(Float.toHexString(x));


Now I'm not able to convert this String result "s" to integer. Please help.

Regards,
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



1. A NumberFormatException will be thrown because the Hex value is floating point, and your trying to convert it to an Integer, a non-floating point data type.

2. A float can't represent a 20 digit number in binary. You lose precision when your print that variable. Nor can a double represent a value that big, so you'll have to use the BigDecimal class.


e.g



3. Unfortunately, I don't see how to convert a BigDecimal object to a Hex String. But, I hope that helps.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't fit that number into an int. An int can't take more than 10 digits.
 
k mayank
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry,

Some how the code I posted was wrong.
float x= 99999999999999999999f; //20 9's
String s = Float.toHexString(x);
System.out.println(s);


Thanks for your reply. I have checked on the BigDecimal class before posting but dont want to write seperate code just to calculate Hexadecimal of a number.
looking for some java API which can do this for me.

Regards,
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about this or this?
 
k mayank
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply.
Both Sound good but as I mentioned in my first post, they dont give back exact Hex string. Float gives 0x1.5af1d8p66 and I was unable to convert it into clean Hexadecimal string.

Regards,



 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I wrote:What about this or this?

Have you tried those links? They will give 256.0f -> 43800000 and -256.0f -> c3800000 which is a "clean" hex result. You do of course have to print the result with the %x formatting tags.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

k mayank wrote:I need to use a float number(because it is 20 or more, digits long), ...


Note that a float (nor a double) can store numbers of 20 digits. The precision of floats and doubles is limited: float can store about 7 decimal digits, and double about 15 decimal digits.

If you really need to preserve all the 20 digits, you should use an arbitrary-precision type such as BigInteger or BigDecimal (look those up in the Java API documentation).
 
k mayank
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Thanks for your replies
This has been really helpful.

Regards,
 
k mayank
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell,

As soon as I thought I have got this done someone pointed out the mistake.
When I give 256.0f, The answer I expect is 100 and not 4380000.

When I try String s = String.format("%x",256L); I get the required result,
But this can only work for long data type. This fails as soon as I give more than 19 digits and I need to work with 20 digita.
Can you let me know how to work through this.
A quick hint by all is very much appretiated as this is bocomming a priprity now.

Regards,
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Somebody has already told you to use java.math.BigInteger; I think that will sort out your problem. It appears in Formatter that the %x tag can be applied to BigInteger.
 
reply
    Bookmark Topic Watch Topic
  • New Topic