float-string to integer

Started by
5 comments, last by GameDev.net 17 years, 7 months ago
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
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
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?

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.
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)
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 :) )
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;



This topic is closed to new replies.

Advertisement