UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on September 15, 2023, 11:00:51 PM

Title: Learn to Code - How to convert a numeric string literal to a positive integer
Post by: kevin on September 15, 2023, 11:00:51 PM
   Learn to Code - How to convert a numeric string literal to a positive integer

    This code basically emulates the PlayBASIC Val() (https://playbasic.com/help.php?page=STRINGS.VAL) and Val#() functions.   Which if you're not familiar will take a string with a numeric value within and convert it do either and Integer or Floating point value.  

    In this version, while not as robust as the VAL() function, we aim to provide a basic understanding of how such functions operate. The purpose of this function is to examine each character in the input string (passed as 's$') one by one.

   When the character is a numeric character, with an ASCII value ranging from zero to nine, we perform the following steps:

   We convert this character into the numeric range of 0 to 9 by subtracting the ASCII value of '0' from it.

   Since we're scanning the string from left to right, starting with the highest digit, we utilize a scaler. Initially set to one, it becomes ten after processing the first character. This scaler allows us to shift existing digits to higher positions, making room for the newly computed digit at the end of the result."




 Summary of the provided PLayBASIC code:

  - The code defines a function called DigitToValue that converts a literal string of digits to an integer value.
  - The function is called multiple times with different input strings.
  - For each input string:
  ---     It initializes result to 0 and Scale to 1.
  ---     It iterates through each character in the input string using a for loop.
  ---     For each character, it checks if it's a digit (0-9) using ASCII values.
  ---     If it's a digit, it multiplies the result by the Scale, then adds the numeric value of the character to the result. The Scale is updated to 10 to handle multi-digit numbers.
  ---     After processing all characters in the string, it prints the original string and the resulting integer value.
  - The function is tested with various input strings, including some with multiple digits.
  - Finally, there are Sync (https://playbasic.com/help.php?page=SCREEN.SYNC) and WaitKey (https://playbasic.com/help.php?page=INPUT.WAITKEY) commands, which typically ensure that any output is displayed on the screen and wait for a keypress before the program exits.    



[pbcode]


    DigitToValue("")
    DigitToValue("0")
    DigitToValue("01")
    DigitToValue("200")
    DigitToValue("9999")
    DigitToValue("99999")
    DigitToValue("999999")
    DigitToValue("9999999")
    DigitToValue("99999999")
    DigitToValue("999999999")
    DigitToValue("9999999999")    ; overflow 32bit integer

   sync
   waitkey
   

Function DigitToValue(s$)

      result=0   
      Scale=1
      for lp =1 to len(S$)
         ThisCHR = Mid(s$,lp)
         if ThisCHR>=asc("0") and ThisCHR<=asc("9")
            result *= Scale
            Result +=ThisCHR-asc("0")
            Scale=10
         endif
      next
      print s$+" -> "+str$(result)   
EndFunction




[/pbcode]


  - Learn to code - Beginner Coding