• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Getting float value from string

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've a string say for example "C2A6AB4B". This is a little endian representation of a float number. How do I get the float out of it using java code?

here is my futile try:

public class Hextofloat implements Serializable
{
private float number =0;
private String hexmsg = null;
private int temp=0;

public float hex2float (String hexMsg){
for(int i=8;i>0;i=i-2){
temp=Integer.parseInt(hexMsg.substring(i-2,i).trim(),16);
number = (number | temp)<<8;
}
return number;
}

}

it gives error message on the line "number =(number | temp)<<8; saying that the "|" is not defined for float, int. I tried (float, float) same thing.

can someone please help with it or give me a function or something that will do the trick?

thanks a lot in advanced.
[ October 06, 2005: Message edited by: Cowboy Drinkalot ]
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since they are whole numbers, how about doint it with longs or ints, and converting only the final answer to float?

Henry
 
Mkhaya Tini
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks henry.

but how do it do that?

x = (float)y;

would work where x is float and y is long? to exactly copy the bits i was thinking i can only do bit wise or. but i'm not able to do that with float.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Joke names, "handles", and obvious fake names don't fly here at the Ranch. You can change your display name
here.
We take this rule rather seriously. Thanks!

Look at the Integer.parseInt() method -- the two-argument version.
 
Mkhaya Tini
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry. changed display name.

can u please explain the solution a bit though. thanks.
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let back this up a bit... What are we talking about here?

Does the string represent a hex number that you want converted a float? Or does the string actually represent the bit pattern for a float? IEEE floating point format?

In both cases, simply do all your math with int only to parse the bits.

If it is the first case, then cast only the final result to float. If it is the second case, then use the Float.intBitsToFloat() method on the final result to get your float.

Henry

P.S. If it is the second case, how the heck did you get the input in the first place?
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use wrapper class to convert.


Although x = y itself would compile too. You don't need explicit cast for that.
 
Mkhaya Tini
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the string represent a hex number that you want converted a float?

Yes, in little endian format (the least sig bit first). lets see if I can put togather some pieces here. thanks.
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic