LongToString

Syntax:

    stringvar = LongToString(Long_variable)     'supports Long.

Command Availability:

Available on all microcontrollers.

Explanation:

The LongToString function converts a number into a string. number can be any Long variable, or a fixed number constant between 0 and 4294967295 inclusive. For Byte numbers use ByteToString(), for Word numbers use WordToString(), for Integer numbers use IntegerToString(), and for Single numbers use SingleToString().

The string variable stringvar contains the same number, represented as a string. The length of the string returned is 10 characters.

This function is especially useful if a number needs to be added to the end of a string, or if a custom data-sending routine only supports the output of string variables.

This function does not support conversion of hexadecimal number strings.

Note: When calling LongToString(), do not leave a space between the function name and the opening parenthesis — doing so produces a compiler error that is not obvious to diagnose.

    ' use this -- no space between LongToString and the opening parenthesis
    LongToString(number_variable)

    ' do not use this -- note the space before the parenthesis
    LongToString (number_variable)

Example 1:

    'Set chip model
    #chip 16F1936

    'Set up hardware serial connection
    #define USART_BAUD_RATE 9600
    #define USART_TX_BLOCKING

    Dim SensorReading as Long

    'Take an A/D reading
    SensorReading = ReadAD12(AN0)

    'Create a string variable
    Dim OutVar As String

    'Fill string with sensor reading
    OutVar = LongToString(SensorReading)          ' <<< the LongToString instruction

    'Send
    HSerPrint OutVar
    HSerPrintCRLF

Key line: OutVar = LongToString(SensorReading) — converts the long-sized ADC reading into a decimal string, ready to be transmitted over the serial connection with HSerPrint.

Example 2:

    '''************************************************************************
    '''  PIC: 16F18855
    '''  Compiler: GCB
    '''  IDE: GCode
    '''  Board: Xpress Evaluation Board
    '''  Date: June 2021

    ' ----- Configuration
      'Chip Settings.
      #chip 16f18855,32
      #Config CLRE_ON
      #option Explicit

    ; ----- Define Hardware settings

      '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 Longvar as Long

    ; ----- Main body of program commences here.
    Longvar = 0xffffffff

    do
       wait 100 ms

       HSerPrint LongToString( Longvar )          ' <<< the LongToString instruction
       HSerPrintCRLF
       wait 1 s
    loop
    end

Key line: HSerPrint LongToString( Longvar ) — converts the maximum Long value 0xffffffff to a string and prints it directly, without needing an intermediate string variable.

See Also: