KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q26931: VAL("&H") and VAL("%") Unexpectedly Returning Nonzero Value

Article: Q26931
Product(s): See article
Version(s): 4.00 4.00b 4.50
Operating System(s): MS-DOS
Keyword(s): ENDUSER | buglist4.00 buglist4.00b buglist4.50 B_BasicCom | mspl13_basic
Last Modified: 31-JAN-1990

When "&H", "&", and "%" are passed alone as arguments to the VAL
function, VAL returns 0 (zero) as expected in QuickBASIC Version 3.00.
In QuickBASIC Version 4.00, VAL returns a nonzero value for "&H" and
"&", and returns a "type mismatch" (error 5) at run time for "%". VAL
is expected to return 0 (zero) because "&H" is 0 in hexadecimal
notation, "&" is 0 in long integer notation, and "%" is 0 in integer
notation.

Passing "&H0" or "0%" to VAL returns 0 as expected.

Microsoft has confirmed this to be a problem in QuickBASIC Versions
4.00, 4.00b, and 4.50 and in Microsoft BASIC Compiler Versions 6.00
and 6.00b (buglist6.00, buglist6.00b). This problem was corrected in
Microsoft BASIC Professional Development System (PDS) Version 7.00 for
MS-DOS and MS OS/2 (fixlist7.00).

If your program accepts input into a string variable and then uses
VAL, you may want to filter out the character combinations that are
not accepted. On the Microsoft QuickBASIC Versions 4.00 and 4.00b
Utilities and Examples Disk in the \Source subdirectory there is a
program called STRTONUM.BAS that filters out all characters from a
string except numeric digits, the decimal point, and a minus sign.
STRTONUM.BAS can be modified if you want to allow hexadecimal
constants but exclude the special cases "&H" and "H" that return
nonzero values from VAL. You can also write your own filtering
routine.

The presence of a coprocessor makes no difference. The following is
a code example:

' The following two statements print 203 in the QuickBASIC Version
' 4.00 editor; they print 233 compiled to EXE with BRUN40.LIB, print
' 1889 with BCOM40.LIB, and print 0 (zero) in QuickBASIC Version 3.00:

PRINT VAL("&h")
PRINT VAL("&")

' The following statement gives "type mismatch" in QuickBASIC Version
' 4.00 editor or EXE; it prints 0 (zero) in QuickBASIC Version 3.00:

PRINT VAL("%")

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.