# float-string to integer

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

## 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 on other sites
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 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 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 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 on other sites
Quote:
 Original post by AshleysBrainYou 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 on other sites
Quote:
Original post by Ludi83
Quote:
 Original post by AshleysBrainYou 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
f = float(i);
f /= 1000000;

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 14
• 14
• ### Forum Statistics

• Total Topics
633385
• Total Posts
3011605
• ### Who's Online (See full list)

There are no registered users currently online

×