QBasic / QB64 Discussion Forum      Other Subforums, Links and Downloads

 Return to Index  

VAL("&H") bug in QBasic 4.5 (but not in QB 1 or PDS 7)

January 12 2007 at 10:14 AM
Solitaire  (Login Solitaire1)

Article documenting this bug is found at url:


The content is reproduced here:

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

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 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 "&H" and "H" that return nonzero values from VAL. You can also write your own filtering routine.


The article refers to the STRTONUM.BAS source code found in the QB4.5 EXAMPLES folder. If you don't have a copy of the source code, here it is:


DECLARE FUNCTION Filter$ (Txt$, FilterString$)

' Input a line:
LINE INPUT "Enter a number with commas: ", A$

' Look for only valid numeric characters (0123456789.-) in the
' input string:
CleanNum$ = Filter$(A$, "0123456789.-")

' Convert the string to a number:
PRINT "The number's value = "; VAL(CleanNum$)
' ========================== FILTER ==========================
'         Takes unwanted characters out of a string by
'        comparing them with a filter string containing
'             only acceptable numeric characters
' ============================================================
FUNCTION Filter$ (Txt$, FilterString$) STATIC
   Temp$ = ""
   TxtLength = LEN(Txt$)

   FOR I = 1 TO TxtLength     ' Isolate each character in
      C$ = MID$(Txt$, I, 1)   ' the string.

      ' If the character is in the filter string, save it:
      IF INSTR(FilterString$, C$) <> 0 THEN
         Temp$ = Temp$ + C$
      END IF

   Filter$ = Temp$

 Respond to this message   

 Copyright © 1999-2014 Network54. All rights reserved.   Terms of Use   Privacy Statement