Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > convert HEX to double

Reply
Thread Tools

convert HEX to double

 
 
puzey@yahoo.com
Guest
Posts: n/a
 
      03-29-2006
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

 
Reply With Quote
 
 
 
 
Thomas Schodt
Guest
Posts: n/a
 
      03-29-2006
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/....String,%20int)

http://java.sun.com/j2se/1.4.2/docs/...l#doubleValue()
 
Reply With Quote
 
 
 
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      03-29-2006
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/...g/java/gui/faq
http://www.uni-giessen.de/faq/archiv....java.gui.faq/
 
Reply With Quote
 
puzey@yahoo.com
Guest
Posts: n/a
 
      03-29-2006
Thanks, Thomas Weidenfeller! It seems work.

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

Have a nice day!

Puze

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-29-2006
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Help: Double convert to Integer and Double.... da Vinci C++ 5 07-31-2004 12:35 AM
How to convert an hex string to a Hex number chirs Javascript 3 12-01-2003 10:06 PM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57