Skip to main content
gamedev.net
🔒 Locked

float-string to integer

Started by Ludi83 5 replies 1.5k views
Original Post
Ludi83
Ludi83
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
Ezbez
Ezbez
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
Ludi83
Ludi83
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?

Maddi
Maddi
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.
AshleysBrain
AshleysBrain
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.
Construct (Free open-source game creator)
Ludi83
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 :) )

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.