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, 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 = 24HJ128GP502 ' Select the device to compile for
Declare Xtal = 16
'
' Create a byte array to hold converted value, and null terminator
Dim MyString as String * 12
Dim MyWord1 as Word
DelayMs 100 ' Wait for things to stabilise
Cls ' Clear the LCD
MyWord1 = 1234 ' Load the variable with a value
Strn MyString = Str$(Dec MyWord1) ' Convert the Integer to a String
Print MyString ' Display the string
Example 2
' Convert a Dword variable to a String of characters in a Byte array.
Device = 24HJ128GP502 ' Select the device to compile for
Declare Xtal = 16
Dim MyString as String * 12
Dim MyDword1 as Dword
DelayMs 100 ' Wait for things to stabilise
Cls ' Clear the LCD
MyDword1 = 1234 ' Load the variable with a value
Strn MyString = Str$(Dec MyDword1) ' Convert the Integer to a String
Print MyString ' Display the string
Example 3
' Convert a Float variable to a String of characters in a Byte array.
Device = 24HJ128GP502 ' Select the device to compile for
Declare Xtal = 16
Dim MyString as String * 12
Dim MyFloat1 as Float
DelayMs 100 ' Wait for things to stabilise
Cls ' Clear the LCD
MyFloat1 = 3.14 ' Load the variable with a value
Strn MyString = Str$(Dec MyFloat1) ' Convert the Float to a String
Print MyString ' Display the string
Example 4
' Convert a Word variable to a Binary String of characters in an array.
Device = 24HJ128GP502 ' Select the device to compile for
Declare Xtal = 16
Dim MyString as String * 32
Dim MyWord1 as Word
DelayMs 100 ' Wait for things to stabilise
Cls ' Clear the LCD
MyWord1 = 1234 ' Load the variable with a value
Strn MyString = Str$(Bin MyWord1) ' Convert the Integer to a String
Print MyString ' Display the 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.

