Results 1 to 8 of 8
  1. #1
    SKaur is offline Member
    Join Date
    Jan 2008
    Posts
    8
    Rep Power
    0

    Default 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

  2. #2
    afsina is offline Member
    Join Date
    Jan 2008
    Posts
    24
    Rep Power
    0

    Default

    Does Float.toHexString(float f) works for you? it is available Java 5 or further

  3. #3
    SKaur is offline Member
    Join Date
    Jan 2008
    Posts
    8
    Rep Power
    0

    Default

    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

  4. #4
    afsina is offline Member
    Join Date
    Jan 2008
    Posts
    24
    Rep Power
    0

    Default

    Use Double and Longs then. they are 64 bits.

  5. #5
    SKaur is offline Member
    Join Date
    Jan 2008
    Posts
    8
    Rep Power
    0

    Default

    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

  6. #6
    afsina is offline Member
    Join Date
    Jan 2008
    Posts
    24
    Rep Power
    0

    Default

    Long.toHexString(Double.doubleToLongBits(37.22d))
    gives me 40429c28f5c28f5c

  7. #7
    afsina is offline Member
    Join Date
    Jan 2008
    Posts
    24
    Rep Power
    0

    Default

    Another way of doing is:

    String s = String.format("&#37;x",Double.doubleToLongBits(37. 22d));

  8. #8
    SKaur is offline Member
    Join Date
    Jan 2008
    Posts
    8
    Rep Power
    0

    Default

    Hi afsina,

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

    Thanx

Similar Threads

  1. rounding double to two decimal places
    By javaMike in forum Advanced Java
    Replies: 15
    Last Post: 03-10-2010, 12:04 AM
  2. Replies: 0
    Last Post: 04-06-2008, 07:40 PM
  3. convert string to a double?
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-27-2007, 03:10 AM
  4. Replies: 2
    Last Post: 11-16-2007, 01:24 PM
  5. Help with convert a double type number
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •