Positron Compiler Documentation

Val

Source: Positron16 Compiler User Manual, PDF page 378

Syntax

Assignment Variable = Val (Array Variable, Modifier)

Overview

Convert a Byte Array or String containing Decimal, Hex, or Binary numeric text into its integer equivalent.

Parameters

Array Variable is a byte array or string containing the alphanumeric digits to convert and terminated by a null (i.e. value 0). Modifier can be Hex, Dec, or Bin. To convert a Hex string, use the Hex modifier, for Binary, use the Bin modifier, for Decimal use the Dec modifier. Assignment Variable is a variable that will contain the converted value. FloatIng point characters and variables cannot be converted, and will be rounded down to the nearest integer value.

Example 1

' Convert a string of hexadecimal characters to an integer
  Device = 24HJ128GP502
                                   ' Select the device to compile for
  Declare Xtal = 16
  Dim String1 as String * 10
                                   ' Create a String
  Dim MyWord as Word
                                   ' Create a variable to hold result
  DelayMs 100
                                   ' Wait for things to stabilise
  Cls
                                   ' Clear the LCD
  String1 = "12AF"
                                   ' Load the String with Hex ASCII
  MyWord = Val(String1,Hex)
                                   ' Convert the String into an integer
  Print Hex MyWord
                                   ' Display the integer as Hex

Example 2

' Convert a string of decimal characters to an integer
  Device = 24HJ128GP502
                                   ' Select the device to compile for
  Declare Xtal = 16
  Dim String1 as String * 10
                                   ' Create a String
  Dim MyWord as Word
                                   ' Create a variable to hold result
  DelayMs 100
                                   ' Wait for things to stabilise
  Cls
                                   ' Clear the LCD
  String1 = "1234"
                                   ' Load the String with Decimal ASCII
  MyWord = Val(String1,Dec)
                                   ' Convert the String into an integer
  Print Dec MyWord
                                   ' Display the integer as Decimal

Example 3

' Convert a string of binary characters to an integer
  Device = 24HJ128GP502
                                   ' Select the device to compile for
  Declare Xtal = 16
  Dim String1 as String * 17
                                   ' Create a String
  Dim MyWord as Word
                                   ' Create a variable to hold result
  DelayMs 100
                                   ' Wait for things to stabilise
  Cls
                                   ' Clear the LCD
  String1 = "1010101010000000"
                                   ' Load the String with Binary ASCII
  MyWord = Val(String1,Bin)
                                   ' Convert the String into an integer
  Print Bin MyWord
                                   ' Display the integer as Binary

Notes.

The Val command is not recommended inside an expression, as the results are not predictable. However, the Val command can be used within an If-Then, While-Wend, or Repeat-Until construct, but the code produced is not as efficient as using it outside a construct, because the compiler must assume a worst case scenario, and use Dword comparisons.

  Device = 24HJ128GP502
                                        ' Select the device to compile for
  Declare Xtal = 16
  Dim String1 as String * 10
                                        ' Create a String
  DelayMs 100
                                        ' Wait for things to stabilise
  Cls
                                        ' Clear the LCD
  String1 = "123"
                                        ' Load the String with Decimal ASCII
  If Val(String1,Hex) = 123 Then
                                        ' Compare the result
    Print At 1,1,Dec Val (String1,Hex)
  Else
    Print At 1,1,"Not Equal"
  EndIf

See also

Str, Strn, Str$.