Results 1 to 8 of 8
- 01-11-2008, 08:27 PM #1
Member
- Join Date
- Jan 2008
- Posts
- 8
- Rep Power
- 0
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
- 01-11-2008, 10:56 PM #2
Member
- Join Date
- Jan 2008
- Posts
- 24
- Rep Power
- 0
Does Float.toHexString(float f) works for you? it is available Java 5 or further
- 01-11-2008, 11:50 PM #3
Member
- Join Date
- Jan 2008
- Posts
- 8
- Rep Power
- 0
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
- 01-12-2008, 06:01 AM #4
Member
- Join Date
- Jan 2008
- Posts
- 24
- Rep Power
- 0
Use Double and Longs then. they are 64 bits.
- 01-12-2008, 07:34 AM #5
Member
- Join Date
- Jan 2008
- Posts
- 8
- Rep Power
- 0
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
- 01-12-2008, 08:40 PM #6
Member
- Join Date
- Jan 2008
- Posts
- 24
- Rep Power
- 0
Long.toHexString(Double.doubleToLongBits(37.22d))
gives me 40429c28f5c28f5c
- 01-12-2008, 08:41 PM #7
Member
- Join Date
- Jan 2008
- Posts
- 24
- Rep Power
- 0
Another way of doing is:
String s = String.format("%x",Double.doubleToLongBits(37. 22d));
- 01-12-2008, 09:02 PM #8
Member
- Join Date
- Jan 2008
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
rounding double to two decimal places
By javaMike in forum Advanced JavaReplies: 15Last Post: 03-10-2010, 12:04 AM -
How to convert integer to the hexadecimal and octal number
By Java Tip in forum java.langReplies: 0Last Post: 04-06-2008, 07:40 PM -
convert string to a double?
By javaMike in forum Advanced JavaReplies: 2Last Post: 11-27-2007, 03:10 AM -
type mismatch: cannot convert from double to float
By bugger in forum New To JavaReplies: 2Last Post: 11-16-2007, 01:24 PM -
Help with convert a double type number
By trill in forum New To JavaReplies: 1Last Post: 08-06-2007, 08:48 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks