aspose file tools
The moose likes Java in General and the fly likes Getting float value from string Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Getting float value from string" Watch "Getting float value from string" New topic
Author

Getting float value from string

Mkhaya Tini
Greenhorn

Joined: Oct 06, 2005
Posts: 23
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 ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16695
    
  19

Since they are whole numbers, how about doint it with longs or ints, and converting only the final answer to float?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mkhaya Tini
Greenhorn

Joined: Oct 06, 2005
Posts: 23
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24061
    
  13

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.


[Jess in Action][AskingGoodQuestions]
Mkhaya Tini
Greenhorn

Joined: Oct 06, 2005
Posts: 23
sorry. changed display name.

can u please explain the solution a bit though. thanks.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16695
    
  19

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?
Ronnie Ho
Ranch Hand

Joined: Aug 10, 2005
Posts: 47
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

Joined: Oct 06, 2005
Posts: 23
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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Getting float value from string
 
Similar Threads
Printing out Ascii charactors
breaks in converting String to Integer
SoDuko puzzle
Int[][] magically changes
Knight's tour