Shop OBEX P1 Docs P2 Docs Learn Events
What does this method do? — Parallax Forums

What does this method do?

lardomlardom Posts: 1,659
edited 2011-11-30 11:08 in Propeller 1
The code below is from HyperTerminal which I don't think is found in PST but I'm puzzled about its purpose. Can anyone present an example showing what it does?

[HTML]PUB StrToDec(stringptr) : value | char, index, multiply

'' Converts a zero terminated string representation of a decimal number to a value
value := index := 0
repeat until ((char := byte[stringptr][index++]) == 0)
if char => "0" and char =< "9"
value := value * 10 + (char - "0")
if byte[stringptr] == "-"
value := - value[/HTML]

This stuff is not always obvious to me. Only yesterday it occurred to me that the "GetDec" method waits for an "enter" keypress as opposed to "Dec" which prints a value or a variable. The two methods are not completely interchangeable. The "GetDec" method has some code I still don't understand yet but at least I know what it does.
With the "StringToDec" method, once I know what it does then how it works will make more sense to me.

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-11-30 07:57
    That routine takes the address of a zero-terminated string and converts the digits contained therein to a number. If the fiirst character in the string is a "-", the number returned is negated. If the string contains groups of digits separated by non-digit characters, the value returned is the same as if the digits were all contiguous.

    -Phil
  • lardomlardom Posts: 1,659
    edited 2011-11-30 08:14
    Thanks, Phil. That paints a clear picture.
  • Heater.Heater. Posts: 21,230
    edited 2011-11-30 09:02
    Why is it called StrToDec?
    Whilst the input string is intended to be a decimal representation of a signed number in ASCII the returned result is a regular signed integer in binary.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-11-30 09:03
    Larry,

    Parallax Serial Terminal.spin has a similar method. The PST method "StrToBase" lets you specify which base system the number is in.
    PRI StrToBase(stringptr, base) : value | chr, index
    {Converts a zero terminated string representation of a number to a value in the designated base.
    Ignores all non-digit characters (except negative (-) when base is decimal (10)).}
      value := index := 0
      repeat until ((chr := byte[stringptr][index++]) == 0)
        chr := -15 + --chr & %11011111 + 39*(chr > 56)                              'Make "0"-"9","A"-"F","a"-"f" be 0 - 15, others out of range     
        if (chr > -1) and (chr < base)                                              'Accumulate valid values into result; ignore others
          value := value * base + chr                                                  
      if (base == 10) and (byte[stringptr] == "-")                                  'If decimal, address negative sign; ignore otherwise
        value := - value
           
    

    If you use ten as the base, then it behaves the same as StrToDec. But you could also use 16 as the base for hexadecimal numbers. It will also convert other base systems. I wish Parallax hadn't made it a private method. I think it's a very useful method (and much cleaner than the method I came up with).
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-11-30 09:09
    Heater. wrote: »
    Why is it called StrToDec?
    Whilst the input string is intended to be a decimal representation of a signed number in ASCII the returned result is a regular signed integer in binary.
    The name is a bit misleading. Maybe it should be called AsciiDecimalStringToSignedBinaryInteger, but I think that name exceeds the Spin symbol length limit. :)
  • Heater.Heater. Posts: 21,230
    edited 2011-11-30 09:16
    "atoi" would do as in C libs. Or perhaps "a2i" if you want to be concise. Point is the result is not decimal, the input is.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-11-30 09:34
    "ASCII decimal", if you want to be pedantic about it (as opposed to, say, packed BCD or EBCDIC). :)

    -Phil
  • Heater.Heater. Posts: 21,230
    edited 2011-11-30 09:59
    Not so pedantic but this gets very confusing.
    Packed BCD would be a representation in the output binary format.
    EBCDIC would be the old alternative to ASCII (from IBM) in the input string if I remember correctly.
  • Heater.Heater. Posts: 21,230
    edited 2011-11-30 09:59
    Bah, duplicate post that I can't delete on my phone for some reason.
  • lardomlardom Posts: 1,659
    edited 2011-11-30 10:03
    My new stated rule is read with questions and a pen. I see that "StringToDec/StringToBase" is a necessary piece in the chain of logic. Reading the replies demonstrates I've got an awful lot to learn. What is meant by a "...packed BCD or EBCDIC"?
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-11-30 10:59
    lardom wrote:
    What is meant by a "...packed BCD or EBCDIC"?
    Packed BCD (binary-coded decimal) is a representation that contains two digits per byte, each encoded in four bits, and ranging from 0000 (0) to 1001 (9).

    EBCDIC (extended binary-coded decimal interchange code) is an old IBM alternative to ASCII that was used on punch cards.

    -Phil
  • lardomlardom Posts: 1,659
    edited 2011-11-30 11:08
    Thanks Heater and Phil Pilgrim. I saw hexadecimal numbers represented by 4 bits on YouTube recently. I will watch it again with a pen this time.
Sign In or Register to comment.