Microsoft KB Archive/26931

= VAL(&quot;&H&quot;) and VAL(&quot;%&quot;) Unexpectedly Returning Nonzero Value =

Article ID: 26931

Article Last Modified on 11/21/2006

-

APPLIES TO


 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS

-



This article was previously published under Q26931



SYMPTOMS
When &quot;&H&quot;, &quot;&&quot;, and &quot;%&quot; 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 &quot;&H&quot; and &quot;&&quot;, and returns a &quot;type mismatch&quot; (error 5) at run time for &quot;%&quot;. VAL is expected to return 0 (zero) because &quot;&H&quot; is 0 in hexadecimal notation, &quot;&&quot; is 0 in long integer notation, and &quot;%&quot; is 0 in integer notation.

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



STATUS
Microsoft has confirmed this to be a bug 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 &quot;&H&quot; and &quot;H&quot; that return nonzero values from VAL. You can also write your own filtering routine.



MORE INFORMATION
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(&quot;&h&quot;) PRINT VAL(&quot;&&quot;)

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

PRINT VAL(&quot;%&quot;)

Additional query words: QuickBas BasicCom buglist4.00 buglist4.00b buglist4.50 B_BasicCom

Keywords: KB26931

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.