Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-11-2008, 10:27 PM
Member
 
Join Date: Jan 2008
Posts: 8
SKaur is on a distinguished road
How do I convert a decimal value to hexadecimal with double precision (64 bit)
I want to convert a decimal value to a hex value with double precision. For example: 37.22 would convert to : 40429C28F5C28F5C

I found this webpage that does the conversion. IEEE-754 Floating-Point Conversion from Floating-Point to Hexadecimal


How would I code a function to do this in Java?

Thanks.

Sukhpreet Kaur
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-12-2008, 12:56 AM
Member
 
Join Date: Jan 2008
Posts: 24
afsina is on a distinguished road
Does Float.toHexString(float f) works for you? it is available Java 5 or further
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-12-2008, 01:50 AM
Member
 
Join Date: Jan 2008
Posts: 8
SKaur is on a distinguished road
Hi there,

Float.toHexString doesn't give me the required answer,

I have tried the following piece of code which gives result in 32-bit but I am looking for the result in 64-bit ....


public class HextoDbl
{
public static void main(String[] args)
{
float f = 37.22f;
int bits, s, e, m;

bits = Float.floatToIntBits(f);
s = ((bits >> 31) == 0) ? 1 : -1;
e = ((bits >> 23) & 0xff);
m = (e == 0) ?
(bits & 0x7fffff) << 1 :
(bits & 0x7fffff) | 0x800000;
String hexStr = Integer.toHexString(bits);
String leadingZeros = "";
System.out.println(hexStr.length()+" length, value: "+hexStr);
switch(hexStr.length()) {

case 7: leadingZeros = "0"; break;
case 6: leadingZeros = "00"; break;
case 5: leadingZeros = "000"; break;
case 4: leadingZeros = "0000"; break;
case 3: leadingZeros = "00000"; break;
case 2: leadingZeros = "000000"; break;
case 1: leadingZeros = "0000000"; break;
case 0: leadingZeros = "00000000"; break;


}
hexStr = leadingZeros + hexStr;
System.out.println( " Hexadecimal: " + hexStr);
}

}


Can anyone help please.

Thank you.

Regards,
Sukhpreet Kaur
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-12-2008, 08:01 AM
Member
 
Join Date: Jan 2008
Posts: 24
afsina is on a distinguished road
Use Double and Longs then. they are 64 bits.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-12-2008, 09:34 AM
Member
 
Join Date: Jan 2008
Posts: 8
SKaur is on a distinguished road
Hi afsina,

I have used Long.toHexString(Double.doubleToLongBits(37.22)) and it gives me the following answer, 40429c2900000000... which is incomplete .... the complete answer should be 40429C28F5C28F5C.

Any suggestions please.

Thank you.

Regards,
Sukhpreet Kaur
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-12-2008, 10:40 PM
Member
 
Join Date: Jan 2008
Posts: 24
afsina is on a distinguished road
Long.toHexString(Double.doubleToLongBits(37.22d))
gives me 40429c28f5c28f5c
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-12-2008, 10:41 PM
Member
 
Join Date: Jan 2008
Posts: 24
afsina is on a distinguished road
Another way of doing is:

String s = String.format("%x",Double.doubleToLongBits(37.22d) );
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-12-2008, 11:02 PM
Member
 
Join Date: Jan 2008
Posts: 8
SKaur is on a distinguished road
Hi afsina,

Thank you so much... it works...
I appreciate your help.

Thanx
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
rounding double to two decimal places javaMike Advanced Java 7 08-21-2008 03:32 AM
How to convert integer to the hexadecimal and octal number Java Tip java.lang 0 04-06-2008 09:40 PM
convert string to a double? javaMike Advanced Java 2 11-27-2007 05:10 AM
type mismatch: cannot convert from double to float bugger New To Java 2 11-16-2007 03:24 PM
Help with convert a double type number trill New To Java 1 08-06-2007 10:48 AM


All times are GMT +3. The time now is 02:35 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org