ASCII to Long conversion


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2009
    Posts
    41

    Default ASCII to Long conversion

    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

  2. #2
    Join Date
    Mar 2006
    Location
    Pennsylvania, USA.
    Posts
    130


    Did you find this post helpful? Yes | No

    Default

    I'll take a stab, untested, but does compile,

    Code:
    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
    If your oscilloscope costs more than your car...

  3. #3
    Join Date
    Feb 2009
    Posts
    41


    Did you find this post helpful? Yes | No

    Default

    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

  4. #4
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

  5. #5
    Join Date
    Feb 2009
    Posts
    41


    Did you find this post helpful? Yes | No

    Default

    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

  6. #6
    Join Date
    Feb 2009
    Posts
    41


    Did you find this post helpful? Yes | No

    Default

    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?

  7. #7
    Join Date
    Feb 2009
    Posts
    41


    Did you find this post helpful? Yes | No

    Default

    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
    Last edited by TerdRatchett; - 6th March 2009 at 00:06.

Similar Threads

  1. High Resolution Timer & Speed Calculator
    By WOZZY-2010 in forum Code Examples
    Replies: 4
    Last Post: - 7th February 2010, 16:45
  2. Convert long binary number to ASCII Decimal
    By Joe Rocci in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 9th July 2009, 13:58
  3. SerIn2 ASCII Conversion
    By rwskinner in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 4th March 2008, 21:41
  4. ascii conversion help needed
    By muddy0409 in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 13th January 2007, 06:33
  5. Conversion of 10-bit word into ASCII
    By Barry Johnson in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 16th January 2005, 14:26

Members who have read this thread : 1

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts