Str$
Syntax
Str Byte Array = Str$ (Modifier Variable) or String = Str$ (Modifier Variable)
Overview
Convert a Decimal, Hex, Binary, or Floating Point value or variable into a null terminated string held in a Byte array, or a String variable. For use only with the Str and Strn commands, and real String variables.
Parameters
Modifier is one of the standard modifiers used with Print, Rsout, HSerout etc. See list below. Variable is a variable that holds the value to convert. This may be a Bit, Byte, Word, Long, Dword, or Float. Byte Array must be of sufficient size to hold the resulting conversion and a terminating null character (0). String must be of sufficient size to hold the resulting conversion.
Notice that there is no comma separating the Modifier from the Variable. This is because the compiler borrows the format and subroutines used in Print. Which is why the modifiers are the same: -
| Bin{1..32} | Convert to binary digits |
| Dec{1..10} | Convert to decimal digits |
| Hex{1..8} | Convert to hexadecimal digits |
| Sbin{1..32} | Convert to signed binary digits |
| Sdec{1..10} | Convert to signed decimal digits |
| Shex{1..8} | Convert to signed hexadecimal digits |
| Ibin{1..32} | Convert to binary digits with a preceding '%' identifier |
| Idec{1..10} | Convert to decimal digits with a preceding '#' identifier |
| Ihex{1..8} | Convert to hexadecimal digits with a preceding '$' identifier |
| ISbin{1..32} | Convert to signed binary digits with a preceding '%' identifier |
| ISdec{1..10} | Convert to signed decimal digits with a preceding '#' identifier |
| IShex{1..8} | Convert to signed hexadecimal digits with a preceding '$' identifier |
Example 1
' Convert a Word variable to a String of characters in a Byte array.
Device = 18F26K40 ' A suitable device for Strings
Declare Xtal = 16 ' Tell the compiler the device is operating at 16MHz
Declare Hserial_Baud = 9600
'
' Create a byte array to hold converted value, and null terminator
'
Dim String1[11] as Byte
Dim Wrd1 as Word
Wrd1 = 1234 ' Load the variable with a value
Strn String1 = Str$(Dec Wrd1) ' Convert the Integer to a String
HRsoutLn Str String1 ' Display the string
Stop
Example 2
' Convert a Dword variable to a String of characters in a Byte array.
Device = 18F26K40 ' A suitable device for Strings
Declare Xtal = 16 ' Tell the compiler the device is operating at 16MHz
Declare Hserial_Baud = 9600
'
' Create a byte array to hold converted value, and null terminator
'
Dim MyString[11] as Byte
Dim Dwd1 as Dword
Dwd1 = 1234 ' Load the variable with a value
Strn MyString = Str$(Dec Dwd1) ' Convert the Integer to a String
HRsoutLn Str MyString ' Display the array acting as a string
Example 3
' Convert a Float variable to a String of characters in a Byte array.
Device = 18F26K40 ' A suitable device for Strings
Declare Xtal = 16 ' Tell the compiler the device is operating at 16MHz
Declare Hserial_Baud = 9600
'
' Create a byte array to hold converted value, and null terminator
'
Dim MyString[11] as Byte
Dim Flt1 as Float
Flt1 = 3.14 ' Load the variable with a value
Strn MyString = Str$(Dec Flt1) ' Convert the Float to a String
HRsoutLn Str MyString ' Display the array acting as a string
Example 4
' Convert a Word variable to a Binary String of characters in an array.
Device = 18F26K40 ' A suitable device for Strings
Declare Xtal = 16 ' Tell the compiler the device is operating at 16MHz
Declare Hserial_Baud = 9600
'
' Create a byte array to hold converted value, and null terminator
'
Dim MyString[34] as Byte
Dim MyWord as Word
MyWord = 1234 ' Load the variable with a value
Strn MyString = Str$(Bin MyWord) ' Convert the Integer to a String
HRsoutLn Str MyString ' Display the array acting as a string
If we examine the resulting string (Byte Array) converted with example 2, it will contain: -
character 1, character 2, character 3, character 4, 0
The zero is not character zero, but value zero. This is a null terminated string.
Notes
The Byte Array created to hold the resulting conversion, must be large enough to accommodate all the resulting digits, including a possible minus sign and preceding identifying character. %, $, or # if the I version modifiers are used. The compiler will try and warn you if it thinks the array may not be large enough, but this is a rough guide, and you as the programmer must decide whether it is correct or not. If the size is not correct, any adjacent variables will be overwritten, with potentially catastrophic results.
See also : Creating and using Strings, Strn, Arrays as Strings.

