Val
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 variable 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 Hexadecimal 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 = 18F452
' A suitable device for Strings
Declare Xtal = 16
' Tell the compiler the device is operating at 16MHz
Declare Hserial_Baud = 9600
Dim String1[10] as Byte
' Create a byte array as a String
Dim Wrd1 as Word
' Create a variable to hold result
Str String1 = "12AF",0
' Load the String with Hex digits
Wrd1 = Val(String1,Hex)
' Convert the String into an integer
HRsoutLn Hex Wrd1
' Display the integer as Hex
Example 2
' Convert a string of decimal characters to an integer
Device = 18F452
' A suitable device for Strings
Declare Xtal = 16
' Tell the compiler the device is operating at 16MHz
Declare Hserial_Baud = 9600
Dim String1[10] as Byte
' Create a byte array as a String
Dim Wrd1 as Word
' Create a variable to hold result
Str String1 = "1234",0
' Load the String with Decimal digits
Wrd1 = Val(String1,Dec)
' Convert the String into an integer
HRsoutLn Dec Wrd1
' Display the integer as Decimal
Example 3
' Convert a string of binary characters to an integer
Device = 18F452
' A suitable device for Strings
Declare Xtal = 16
' Tell the compiler the device is operating at 16MHz
Declare Hserial_Baud = 9600
Dim String1[17] as Byte
' Create a byte array as a String
Dim Wrd1 as Word
' Create a variable to hold result
Str String1 = "1010101010000000",0 ' Load the String with Binary
Wrd1 = Val(String1,Bin)
' Convert the String into an integer
HRsoutLn Bin Wrd1
' Display the integer as Binary
Notes
There are limitations with the Val command when used on a 14-bit core device, in that the array must fit into a single RAM bank. But this is not really a problem, just a little thought when placing the variables will suffice. The compiler will inform you if the array is not fully located inside a Bank, and therefore not suitable for use with the Val command.
This is not a problem with 18F devices, as they are able to access all their memory very easily.
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 = 18F452
' A suitable device for Strings
Declare Xtal = 16
' Tell the compiler the device is operating at 16MHz
Declare Hserial_Baud = 9600
Dim String1[10] as Byte
' Create a byte array as a String
Str String1 = "123",0
' Load the String with Dec digits
If Val(String1,Hex) = 123 Then
' Compare the result
HRsoutLn Dec Val (String1,Hex)
Else
HRsoutLn "not Equal"
EndIf
Stop