ASCII to BINARY help needed (Satellite tracking project)
Feb 25, 2004 2 Replies
A
Andrew Rich
Howdy,
I have a satellite tracking program that outputs from AZ,1-359,EL,1-90 in ASCII.
I am just wondering how I can convert this to something meaningfull in the PIC.
example How would I convert '2' '8' '0' ASCII to a binary equivalent ?
See I want to compare this to the counter in the PIC and then do something.
Work out if the incoming value is higher or lower than the current counter value.
Ideas ?
-- Andrew Rich (VK4TEC)
formatting link
snipped-for-privacy@hotmail.com
space - electronics - radio - aviation
-- Andrew Rich (VK4TEC)
formatting link
snipped-for-privacy@hotmail.com
space - electronics - radio - aviation
Didn't find your answer? Ask the community — no account required.
G
Gary Kato
Subtracting ASCII 0 turns an ASCII digit into a binary digit. From there, it's
*100 + *10 + .
B
Ben Jackson
Well first consider that 1..359 (or is that 0-359?) takes 9 bits (or practically speaking, 2 bytes = 16 bits). How much precision do you need? If you represent that in 0..255 you can fit it in one byte and work with it more easily, at a cost of making each degree 'bigger' and losing some accuracy. Or if your working range is smaller (say you only want to work in the 51-173 range) you might want to do part of the comparison in ASCII (reject leading 3, or 2 hundred) and then convert to binary (fitting in 8 bit) and work from there.
However, the general answer to your question is:
AZ = 0 for each ASCII byte: AZ = AZ * 10 AZ = AZ + byte - "0" ; that's the ASCII value of 0, MPASM ; uses quotes for that, C would use ''
If you are really getting 000 .. 359 (ie always 3 digits) you can do
AZ = 100 * (first ASCII byte - "0") AZ = AZ + 10 * (second ASCII byte - "0") AZ = AZ + third ASCII byte - "0"
If you want to convert and scale at the same time, 256/360=.71111:
AZ = 71 * (first - "0") AZ = AZ + 7 * (second - "0") AZ = AZ + (third - "0") * 3 / 4 ; that's easier pic math than *7/10 AZ = AZ + 1 ; biases better
That's off by at most 1 degree everywhere, which is better than I expected with the *3/4 trick.
Ben Jackson
http://www.ben.com/
Join the Discussion
Have something to add? Share your thoughts — no account required.
Didn't find your answer?
Ask the community — no account required
Report Content
You are reporting this content to the moderators. They will look at it
ASAP.