What does this method do?
lardom
Posts: 1,659
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.
[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
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.
Parallax Serial Terminal.spin has a similar method. The PST method "StrToBase" lets you specify which base system the number is in.
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).
-Phil
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.
EBCDIC (extended binary-coded decimal interchange code) is an old IBM alternative to ASCII that was used on punch cards.
-Phil