Important Notice
The pages on this site contain documentation for very old MS-DOS software,
purely for historical purposes.
If you're looking for up-to-date documentation, particularly for programming,
you should not rely on the information found here, as it will be woefully
out of date.
LTRIM$, RTRIM$, STR$, and VAL Functions Example
◄Example► ◄Contents► ◄Index► ◄Back►
──────────────────────────────────────────────────────────────────────────────
' This example uses VAL and STR$ to convert a hexadecimal number to a
' decimal number. The LTRIM$ and RTRIM$ functions are used to trim blanks
' from the resulting string.
' To try this example:
' 1. Choose New Project from the File menu
' 2. Copy the code example below to the code window
' 3. Press F5 to run the example
CLS ' Clear the screen
' Accept all data as ASCII text and assign it to a string.
LINE INPUT "Enter hexadecimal value to be converted to decimal: "; fathex$
hexnum$ = LTRIM$(RTRIM$(fathex$)) ' Trim blanks off both sides of string
outhex$ = UCASE$(hexnum$) ' Convert characters to uppercase
num# = VAL("&H" + outhex$) ' &H tells VAL that A,B,C,to F are OK
dnum$ = STR$(num#) ' STR$ converts decimal back to string
PRINT "Your input is "; "&H " + outhex$
LOCATE 19, 1
PRINT "Hex input "; "&H " + outhex$; " equals "; dnum$; " in decimal"
PRINT "Decimal value "; dnum$; " equals "; "&H " + outhex$; " in hex"
PRINT
END