convert HEX to double

Discussion in 'Java' started by puzey@yahoo.com, Mar 29, 2006.

  1. Guest

    Hi there,

    Need your expertise here. I am trying to convert a Hex in string format
    to double. e.g.

    String toConvert = "bfdf000000000000";
    Double dblDouble = Double.longBitsToDouble(Long.parseLong(toConvert,
    16));
    System.out.print(dblDouble);

    I got a NumberFormatException because bfdf000000000000 is out of range
    for a long type. But this number is within the range of double. How can
    I convert?

    Many thanks in advance.

    Puze
     
    , Mar 29, 2006
    #1
    1. Advertising

  2. wrote:
    > Hi there,
    >
    > Need your expertise here. I am trying to convert a Hex in string format
    > to double. e.g.
    >
    > String toConvert = "bfdf000000000000";
    > Double dblDouble = Double.longBitsToDouble(Long.parseLong(toConvert,
    > 16));
    > System.out.print(dblDouble);
    >
    > I got a NumberFormatException because bfdf000000000000 is out of range
    > for a long type. But this number is within the range of double. How can
    > I convert?
    >
    > Many thanks in advance.
    >
    > Puze
    >


    http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigInteger.html#BigInteger(java.lang.String, int)

    http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigInteger.html#doubleValue()
     
    Thomas Schodt, Mar 29, 2006
    #2
    1. Advertising

  3. wrote:
    > I got a NumberFormatException because bfdf000000000000 is out of range
    > for a long type. But this number is within the range of double. How can
    > I convert?


    For example by just coding the conversion by hand:


    long result = 0;
    String s = "fbcd000000000000"
    for(int i = 0; i < s.length(); i++) {
    result = (result << 4) | Character.digit(s.charAt(i), 16);
    }

    You need to add error handling if s can be to long or s not necessarily
    includes hex digits only.

    /Thomas
    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
     
    Thomas Weidenfeller, Mar 29, 2006
    #3
  4. Guest

    Thanks, Thomas Weidenfeller! It seems work.

    Thanks also go to Thomas Schodt and others who send me email directly.

    Have a nice day!

    Puze
     
    , Mar 29, 2006
    #4
  5. Roedy Green Guest

    On 29 Mar 2006 06:16:36 -0800, wrote, quoted or
    indirectly quoted someone who said :

    >String toConvert = "bfdf000000000000";
    >Double dblDouble = Double.longBitsToDouble(Long.parseLong(toConvert,
    >16));
    >System.out.print(dblDouble);



    The problem is parseLong is expecting a signed number. The unsigned
    one you give it is too big.

    You can see the problem if you run this code fragment
    String toConvert = "bfdf000000000000";
    long temp = Long.parseLong( toConvert, 16 );
    System.out.println( temp );

    Your code contains another error, masked by boxing. This is closer to
    what you meant:

    long temp = 0xbfdf000000000000L;
    double d = Double.longBitsToDouble( temp );
    System.out.println( d );

    Double.longBitsToDouble returns a double, not a Double.

    I see three ways to solve your unsigned problem:

    1. arrange to get get the bits in binary rather than hex.

    2. construct the low and high 32 bits then shift then OR.

    3. Convert hex chars from unsigned to signed, perhaps when generated
    (see http://mindprod.com/jgloss/hex.html) or by checking for high
    char being >= 8 , replace it with equivalent char <8 with high bit
    off, then mask the sign bit back in afterwards. Conceptually simple,
    but in practice long winded.

    For hints on how to do the details, so the at the code in
    http://mindprod.com/products1.html#LEDATASTREAM
    for readDouble

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Mar 29, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,220
    Neredbojias
    Aug 19, 2005
  2. da Vinci
    Replies:
    5
    Views:
    8,733
    da Vinci
    Jul 31, 2004
  3. Sydex
    Replies:
    12
    Views:
    6,505
    Victor Bazarov
    Feb 17, 2005
  4. Bengt Richter
    Replies:
    6
    Views:
    473
    Juha Autero
    Aug 19, 2003
  5. chirs
    Replies:
    3
    Views:
    167
    Dr John Stockton
    Dec 1, 2003
Loading...

Share This Page