Jump to content
  • Advertisement
Sign in to follow this  
Ludi83

float-string to integer

This topic is 4353 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm currently doing some embedded programming and here's my problem: I have a string with a float value in it, e.g. "12.748392". Now, I have to convert this string into a number. But I can't save it as float (the platform i'm using has only int (32 Bit) available), so I have to put it into an int32 and convert this int32 on my pc back to the float number. And no, I can't send the string itself to my pc, don't ask why ;) Any solutios for that? Greetings

Share this post


Link to post
Share on other sites
Advertisement
That's an awkward situation you got there.

Anyhow, you're still sending bits and bytes across. You could, say, send each digit accross as an integer (with 11 being a decimal point, or something, 12 being a negative sign, mabye), which would be pretty easy to do, but not efficient. Alternatively, you could send four digits across in one integer (it's 32 bits, a char is 8 bits), which would be a bit more work, but faster, if you need to do that alot.

HTH

Share this post


Link to post
Share on other sites
Okay, thanks for the input, Ezbez. Here are some more details:

The string data I'm receiving are gps positions and I send them via gprs to a server.

So I have a certain range. The number infront of the . has a max/min value of 180/-180. And I need a accuracy of 4 to 6 digits behind the ..

As I only have an unsigned int 32 (~2.000.000.000 - 2.000.000.000), I was thinking about that:

First digit will be plus/minus, then the next three digits will be the value between -180/180 and the rest will be the rest of the string.

So, the int would look like this

(0/1)_(0-180)_rest is the value behind the . (6 digits)


I always find the solutions for the problem, after I post them ;) Any other suggestions?

Share this post


Link to post
Share on other sites
Can't you cut the number at the dot ? You could then simply parse the number befor the dot, send it, then send a cut marker, then parse the rest after the dot as a normal int and send it too. On the receiver side, you can then rebuild the float out of these.

Share this post


Link to post
Share on other sites
You mean you can't do this?:
int myInt = *(int*)&myFloat;
It may be possible to write a routine to convert a string to a correctly formatted float, except you're just writing bitwise to a 32 bit integer. Try looking in to the binary format of floats.

Share this post


Link to post
Share on other sites
Quote:
Original post by AshleysBrain
You mean you can't do this?:
int myInt = *(int*)&myFloat;


No, that's impossible. I don't have the datatype float available (only ints and strings). And I'm forced to use Python 1.5.2, so no pointer magic :)

I'm trying to cut the numbers at the dot and put both vaules into one int (as I tried to descripe one of my upper posts :) )

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Ludi83
Quote:
Original post by AshleysBrain
You mean you can't do this?:
int myInt = *(int*)&myFloat;


No, that's impossible. I don't have the datatype float available (only ints and strings). And I'm forced to use Python 1.5.2, so no pointer magic :)

I'm trying to cut the numbers at the dot and put both vaules into one int (as I tried to descripe one of my upper posts :) )



What you could try is multiplying the number by (x) so that you get an integer value
4 Decimal place * 10000
5 Decimal place * 100000

Having a +180 to 6 decimal places gives in hex 0x0aba9500 , which fits nicely into a 32 bit integer. Also negative numbers fit in nicely too.

send
f *= 1000000;
i = int(f)
transmit(i);

get
i = receive();
f = float(i);
f /= 1000000;



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!