Decimal String to Fixed Point Long?

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey guys,

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

  char String[7] = "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!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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[6] = "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.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

...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!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

??? What you live in the mountains or in an aeroplane? I don't want YOU to be MY pilot if your flying with one hand and replying to AVRFreaks messages with the other...

Hehe, I seem to be always on too. However, I DON'T have a life outside of these virtual walls.... ;)

- Dean :twisted:

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