PDA

View Full Version : ASCII to Long conversion



TerdRatchett
- 4th March 2009, 21:46
Hi Guys,

I have some ASCII arrays of [5] left justified and I need to convert them to Longs. I'm not sure how to go about doing that and I think the left justification complicates things. Any suggestions?

Here are a few examples..

mydata[0] = 6 (h36)
mydata[1] = 4 (h34)
mydata[2] = 2 (h32)
mydata[3] = 3 (h33)
mydata[4] = space character (h20)

So the number to go into my long Var would be 6423

mydata[0] = 3 (h33)
mydata[1] = 7 (h37)
mydata[2] = 1 (h31)
mydata[3] = space character (h20)
mydata[4] = space character (h20)

would be 371

Thanks in advance!
TR

b1arrk5
- 5th March 2009, 00:24
I'll take a stab, untested, but does compile,



MyData var byte[5] 'Data array
Multiplier var byte 'Base ten multiplier
Index var byte 'Array index
Multiplier = 1 'Initialize the variable
For Index = 4 to 0 step - 1 'data is left justified, with non digits at right end
If MyData[Index] > 48 AND MyData[index] < 59 Then 'ascii 0 to 9
MyData[Index] = MyData[Index]-48 * Multiplier
Multiplier = [Multiplier * 10] 'ones, tens, hundreds, thousands...
ENDIF
NEXT Index

Then just but the bytes into your long variable in whatever order you like.
Hope this helps,
Jerry

TerdRatchett
- 5th March 2009, 02:22
Hi Jerry,

Thanks! I think I understand, that looks like it could work.

One question though...

How in the devil do you get the bytes to take the form of a long?

TR

Jerson
- 5th March 2009, 03:58
You need to go over this thread again. It has what you need.

http://www.picbasic.co.uk/forum/showthread.php?t=10550&highlight=hserin+parse

TerdRatchett
- 5th March 2009, 06:41
Hi Jerson,

You mean the answer is in one of my own former posts? That's embarrasing :)

At that point thanks to your code slightly modified I was able to parse my serial port data into two arrays rather than longs. I found this was better since I didn't need to do anything other than display it. Now I am having to add a feature where I average the last 60 samples. I don't think there is any way to average ASCII strings so I decided I need to convert it to longs.

So looking back at your original code I'm not totally clear how it works. I'll study it in more detail and maybe it will click...

TR

TerdRatchett
- 5th March 2009, 20:47
I've got the code entered and possibly working, so I created some quick test code to verify it. But I'm not getting the results i expected...

I'm just doing a HSEROUT [longvalue] after forcing known data into the string and calling the sub, but I'm only getting one byte out of the serial port?

TerdRatchett
- 5th March 2009, 23:43
Hi Jerson and Jerry,

I appreciate the help from both of you. I was able to take the parts I liked best from both of your functions and merge them into this....

MyData var byte[5] 'Data array
LongData var long 'Base ten multiplier
Index var byte 'Array index

LongData = 0 'Initialize the variable

For Index = 0 to 4 step 1 ' potential non-digits in MB locations
If MyData[Index] > 47 AND MyData[index] < 58 Then ' allow only ascii digits 0 to 9
LongData = LongData * 10 ' carry it over
LongData = LongData + MyData[Index] - 48 ' subtract away ASCII offset
ENDIF
NEXT Index

HSEROUT [DEC LongData]


I've tested it by feeding it some array data like this:

MyData[0] = "7"
MyData[1] = "1"
MyData[2] = "0"
MyData[3] = "9"
MyData[4] = " "

and the serial port data (the Long) reflects the ASCII data, so it seems like we have success!

Thanks again!
TR