Shop OBEX P1 Docs P2 Docs Learn Events
string to decimal — Parallax Forums

string to decimal

ArchiverArchiver Posts: 46,084
edited 2003-05-13 14:50 in General Discussion
I am communicating with a digital scale via RS232 and using the
Serin command on my BS2. I am getting the info correctly but need
to convert it into decimal value. The script I use is

serData var byte(10)
again:
serin 15,16780,[noparse][[/noparse]STR serdata\9]
pause 50
debug cls, str serdata
goto again

on the debug screen I get +0056.25 which is the correct value (of
course this changes with the load on the scale), the question is how
can I turn this string into a decimal value so that I can use it
inside my script. I don't particularly care about the value after
the decimal value, but would be nice have it.

any help!

Al

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2003-05-13 01:47
    If your string is a fixed length and is structured as in your example, it's
    pretty easy:

    whole = 0
    whole = whole + ((serData(1) - "0") * 1000)
    whole = whole + ((serData(2) - "0") * 100)
    whole = whole + ((serData(3) - "0") * 10)
    whole = whole + (serData(4) - "0")

    frac = 0
    frac = frac + ((serData(6) - "0") * 10)
    frac = frac + (serData(7) - "0")

    I'm sure somebody like Tracy or Al has a more elegant solution, but this will
    work and is should be very easy for you to understand.

    -- Jon Williams
    -- Parallax


    In a message dated 5/12/2003 5:35:16 PM Central Standard Time,
    brownstamp@y... writes:

    > I am communicating with a digital scale via RS232 and using the
    > Serin command on my BS2. I am getting the info correctly but need
    > to convert it into decimal value. The script I use is
    >
    > serData var byte(10)
    > again:
    > serin 15,16780,[noparse][[/noparse]STR serdata\9]
    > pause 50
    > debug cls, str serdata
    > goto again
    >
    > on the debug screen I get +0056.25 which is the correct value (of
    > course this changes with the load on the scale), the question is how
    > can I turn this string into a decimal value so that I can use it
    > inside my script. I don't particularly care about the value after
    > the decimal value, but would be nice have it.
    >
    > any help!
    >
    > Al



    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2003-05-13 02:26
    Thanks Jon but your script didn't work. Here is how I used it:

    serData var byte(10)
    whole var word
    i var nib
    again:
    serin 15,16780,[noparse][[/noparse]STR serdata\9]
    whole = 0
    whole = whole + ((serData(1) - "0") * 1000)
    whole = whole + ((serData(2) - "0") * 100)
    whole = whole + ((serData(3) - "0") * 10)
    whole = whole + (serData(4) - "0")

    pause 50
    debug cls, dec whole
    goto again

    THe result is number 65078 irrelevent of the load! I know that the
    scales is working!

    Al




    --- In basicstamps@yahoogroups.com, jonwms@a... wrote:
    > If your string is a fixed length and is structured as in your
    example, it's
    > pretty easy:
    >
    > whole = 0
    > whole = whole + ((serData(1) - "0") * 1000)
    > whole = whole + ((serData(2) - "0") * 100)
    > whole = whole + ((serData(3) - "0") * 10)
    > whole = whole + (serData(4) - "0")
    >
    > frac = 0
    > frac = frac + ((serData(6) - "0") * 10)
    > frac = frac + (serData(7) - "0")
    >
    > I'm sure somebody like Tracy or Al has a more elegant solution,
    but this will
    > work and is should be very easy for you to understand.
    >
    > -- Jon Williams
    > -- Parallax
    >
    >
    > In a message dated 5/12/2003 5:35:16 PM Central Standard Time,
    > brownstamp@y... writes:
    >
    > > I am communicating with a digital scale via RS232 and using the
    > > Serin command on my BS2. I am getting the info correctly but
    need
    > > to convert it into decimal value. The script I use is
    > >
    > > serData var byte(10)
    > > again:
    > > serin 15,16780,[noparse][[/noparse]STR serdata\9]
    > > pause 50
    > > debug cls, str serdata
    > > goto again
    > >
    > > on the debug screen I get +0056.25 which is the correct value
    (of
    > > course this changes with the load on the scale), the question is
    how
    > > can I turn this string into a decimal value so that I can use it
    > > inside my script. I don't particularly care about the value
    after
    > > the decimal value, but would be nice have it.
    > >
    > > any help!
    > >
    > > Al
    >
    >
    >
    > [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2003-05-13 03:04
    Okay, Al, I participated in a filmmaking "race" that had me going without
    sleep for about 45 hours this weekend -- and I'm still not quite on my feet.
    What you should be able to gleen is an approach to Ascii-to-decimal
    conversion.

    Check to see the positions of the data within the string, then make
    adjustments where needed.

    -- Jon Williams
    -- Parallax

    In a message dated 5/12/2003 8:29:00 PM Central Standard Time,
    brownstamp@y... writes:

    > Thanks Jon but your script didn't work. Here is how I used it:
    >
    > serData var byte(10)
    > whole var word
    > i var nib
    > again:
    > serin 15,16780,[noparse][[/noparse]STR serdata\9]
    > whole = 0
    > whole = whole + ((serData(1) - "0") * 1000)
    > whole = whole + ((serData(2) - "0") * 100)
    > whole = whole + ((serData(3) - "0") * 10)
    > whole = whole + (serData(4) - "0")
    >
    > pause 50
    > debug cls, dec whole
    > goto again
    >
    > THe result is number 65078 irrelevent of the load! I know that the
    > scales is working!
    >
    > Al



    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2003-05-13 03:24
    Sorry Jon, I should have been paying more attention. It works! I
    guess I shouldn't always take what you say as gospel! (but almost)

    Al

    --- In basicstamps@yahoogroups.com, jonwms@a... wrote:
    > Okay, Al, I participated in a filmmaking "race" that had me going
    without
    > sleep for about 45 hours this weekend -- and I'm still not quite
    on my feet.
    > What you should be able to gleen is an approach to Ascii-to-
    decimal
    > conversion.
    >
    > Check to see the positions of the data within the string, then
    make
    > adjustments where needed.
    >
    > -- Jon Williams
    > -- Parallax
    >
    > In a message dated 5/12/2003 8:29:00 PM Central Standard Time,
    > brownstamp@y... writes:
    >
    > > Thanks Jon but your script didn't work. Here is how I used it:
    > >
    > > serData var byte(10)
    > > whole var word
    > > i var nib
    > > again:
    > > serin 15,16780,[noparse][[/noparse]STR serdata\9]
    > > whole = 0
    > > whole = whole + ((serData(1) - "0") * 1000)
    > > whole = whole + ((serData(2) - "0") * 100)
    > > whole = whole + ((serData(3) - "0") * 10)
    > > whole = whole + (serData(4) - "0")
    > >
    > > pause 50
    > > debug cls, dec whole
    > > goto again
    > >
    > > THe result is number 65078 irrelevent of the load! I know that
    the
    > > scales is working!
    > >
    > > Al
    >
    >
    >
    > [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2003-05-13 14:50
    Perhaps more compact:

    I VAR BYTE
    MyVal VAR WORD
    MyStr VAR BYTE(9)
    MaxLen CON 5
    .
    .
    MyVal = 0
    FOR I = 0 TO MaxLen
    MyVal = MyVal * 10
    MyVal = MyVal + MyStr(I) - "0"
    NEXT

    This will work for any number of leading '0'.
    You might want to use a 'while' loop, and stop
    when you find the '.' character.



    --- In basicstamps@yahoogroups.com, "brownstamp" <brownstamp@y...>
    wrote:
    > I am communicating with a digital scale via RS232 and using the
    > Serin command on my BS2. I am getting the info correctly but need
    > to convert it into decimal value. The script I use is
    >
    > serData var byte(10)
    > again:
    > serin 15,16780,[noparse][[/noparse]STR serdata\9]
    > pause 50
    > debug cls, str serdata
    > goto again
    >
    > on the debug screen I get +0056.25 which is the correct value (of
    > course this changes with the load on the scale), the question is
    how
    > can I turn this string into a decimal value so that I can use it
    > inside my script. I don't particularly care about the value after
    > the decimal value, but would be nice have it.
    >
    > any help!
    >
    > Al
Sign In or Register to comment.