## Decimal String to Fixed Point Long?

7 posts / 0 new
Author
Message

Hey guys,

Say I have a string, with a decimal, such as:

```  char String = "123.45";
```

But I want to convert in to an unsigned long, with fixed point maths. I assume there's a simple routine such as atol that will convert numerical string to unsigned longs, but I also need the decimals.

I have very limited codespace left, and so I cannot fit in the floating point libraries, hence the need for my own simple fixed point maths; shifting the variable left and right twice.

How do I convert the string simply and preserve the decimals, without using the flotaing point maths libraries, only fixed point maths?

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

Well, fixed point math, implies that there is a fixed point, but only you know where the point is. So you would have to do something like:

`char String = "12345"; `

And then you can run it through atol() [stdlib.h] to get a long integer, but then you have to keep track of the decimal point (2 digits) when you do the math.

Oh, sorry, I meant to say, the decimal number should ALWAYS be two digits (device will not accept more than two digits and appends a 0 if only one decimal entered). Would atol ignore the decimal and return "12345" for example? If so, this means I could just shift the data two points every time.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

Well, think about it. That function should stop at the first non-digit character, which of course would be the period. So you would get an integer of 123. If you know that you always have 2 decimals, then drop the decimal number in the math (only display it when you have to) but you have to keep track of the decimal movement, such as in multiply operations. It's really application-specific.

...Ok, i'll do that...

Incidentally, how is it your always on, EW? Arn't you at work, or are you in a balmy part of the world where it's the evening or somthing? :)

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

abcminiuser wrote:
...Ok, i'll do that...
Incidentally, how is it your always on, EW?

It's an illusion. I actually do have a life. :)

abcminiuser wrote:

Arn't you at work, or are you in a balmy part of the world where it's the evening or somthing? :)

At an altitude of 8,000 feet / 2,438 meters, I don't think "balmy" is the correct description. :)