StringToSingle

Syntax:

    var = StringToSingle(string)   'Supports decimal Single range strings only.

Command Availability:

Available on all microcontrollers.

Explanation:

The StringToSingle function extracts a number from a string variable and stores it in a Single variable. One potential use is parsing a floating-point value received over a serial connection.

StringToSingle will not extract a value from a hexadecimal string.

The function reports its result via a status variable:

    '   SysByte_STS_Err = 0 if no error
    '   SysByte_STS_Err.0 = 1 good - 0 - bad
    '   SysByte_STS_Err.1 = 1 too many decimal-place characters, 0 = ok
    '   SysByte_STS_Err.2 = 1 too many integer-place characters (out of range), 0 = ok
    '   SysByte_STS_Err.3 = 1 no decimal point, info only
    '   SysByte_STS_Err.4 = non-numeric characters found

Example 1:

    ' ----- Configuration
    'Chip Settings.
    #chip 16f18855,32
    #Config MCLRE_ON

    'Set the PPS of the RS232 ports.
    UNLOCKPPS
      RC0PPS = 0x0010     'RC0->EUSART:TX;
      RXPPS  = 0x0011     'RC1->EUSART:RX;
    LOCKPPS

    ; ----- Constants
    #define USART_BAUD_RATE 19200
    #define USART_TX_BLOCKING

    ; ----- Variables
    dim Singlevar as Single

    Singlevar = 0

    ; ----- Main body of program commences here.

     #option Explicit

     do
         wait 100 ms

         Singlevar = StringToSingle( "255" )          ' <<< the StringToSingle instruction
         HSerPrint SingleToString(Singlevar)
         HSerPrintCRLF

         wait 1 s
      loop
    end

Key line: Singlevar = StringToSingle( "255" ) — parses the decimal digits in the literal string "255" and stores the resulting floating-point value in Singlevar; the following line converts it back to a string with SingleToString so HSerPrint can display it.

See Also:

  • StringToByte — related command in the same category
  • StringToLong — related command in the same category
  • SingleToString — the inverse conversion, used above to display the result