Val

Blitz3D Forums/Blitz3D Programming/Val

From the Blitz Command Reference:
If you wish to convert from a string to a number, there is no equivalent Val command. Instead, simply assign the string variable into a numeric variable, and Blitz will implicitly convert it.


But this doesn't appear to be possible.

a$ = "5"
num = a$

Of course this code won't compile... "Error! Variable type mismatch." ...of course! So how does this work???

Use Int()

Dont know why yer gettin the error as the code above works fine in Blitz, there shouldnt be a need to use Int() as Rottbott mentioned.

Tried it. Doesn't compile either.

What version of Blitz3d are you using? I've tested with v1.88 and it's fine!

Hmmm... I've got the v1.88 compiler installed. I'm using Protean IDE. I wonder if Protean is doing some checking for a type mismatch error. Anybody else using Protean?

You get the variable type mismatch if you have used the "a" variable previously in your code, where it wasn't declared as a string.

Ah ha. Very interesting. I've got it to work now. Thanks for the tip.

Lately I've taken to declaring all my locals, like a good and proper programmer ought to. I think I'll save confusion and just write a Val() function. I wanted it to handle Hex values as well, anyway.

Okay, so I've written a more comprehensive Val() function that converts integers and floats (using the built-in functionality described above) as well as hex and binary strings. Hex values can begin with either "0x" or "#" ...I know there's a standard prefix for binary values, but I can't recall what it is--I've used "$"

Local Value%

Print Val("37")
Print Val("3-7")
Print Val("50.1")
Print Int(Val("0xFFFF"))
Print Val("#FF")
Value = Val("$10010010")
Print Value

End


Function Val#(StringNumeric$)

   Local Num# = 0
   Local Hex1 = (Left$(StringNumeric$,1)="#")
   Local Hex2 = (Left$(StringNumeric$,2)="0x")
   Local Binary = (Left$(StringNumeric$,1)="$")
   Local c
   
   If Hex1 Or Hex2
      StringNumeric$ = Upper(StringNumeric$)
      For c=(Hex1 + (Hex2 * 2) + 1) To Len(StringNumeric$) 
         Select Mid$(StringNumeric$,c,1)
            Case "1","2","3","4","5","6","7","8","9","0"
               Num# = (Num# * 16) + Asc(Mid$(StringNumeric$,c,1))-48
            Case "A","B","C","D","E","F"
               Num# = (Num# * 16) + Asc(Mid$(StringNumeric$,c,1))-55
            Default
               Return Num#                        
         End Select
      Next
   Else
      If Binary
         For c=2 To Len(StringNumeric$) 
            Select Mid$(StringNumeric$,c,1)
               Case "1"
                  Num# = (Num# * 2) + 1
               Case "0"
                  Num# = (Num# * 2)
               Default
                  Return Num#                        
            End Select
         Next
      Else
         Num# = StringNumeric$
      EndIf
   EndIf
   Return Num#
   
End Function


Anybody have any suggestions? Otherwise, I'll post it over in the code archives.

In most BASICs I have seen, $ is standard for HEX, % for binary, 0 for octal and # for explicit decimal (in case the number has a leading 0, it doesn't get confused with octal).

255 ;decimal
#255 ;decimal
%11111111 ;binary
0377 ;octal
$FF ;hex

0xFF is used in C for Hex values
#FF is used in HTML for Hex values

Okay, I'll add:

$FF for Hex
%0011 for Binary

Decimal values shouldn't need any special characters.
A leading zero for octal values doesn't work comfortably to me: 0.35 is a floating value with a leading zero. ...Would I offend anybody by leaving out Octal? Does anybody really use it??

Octal? Isn't that the new Spiderman villian?

(ie. I don't know anyone who uses it, and besides, anyone who IS using it must already have their own OCT routines)

LOL!

My thoughts precisely.

The new version of Val() can be found here:
http://www.blitzbasic.com/codearcs/codearcs.php?code=1189

use 0x00377 for Octal, that would work

That would be interpreted as Hex, for a completely different result.

@TomToad.... err, ive never come across those in BASIC, since the BBC. what versions have you used?

i have a friend who lives on BBC basic, you might as well forget the BBC basic stuff! todays basic is a bit different

'%' for binary? you learn something new every day... I accidently figured out '#' for Hex one day. Twas a long day..

With the old sam coupe, I'm sure it used & for Hexadecimal. I think. Long time. Loved the Sam coupe, could use all your speccy code, but had like 512k RAM (!) and you could use 16 (YEAH! 16!) colours on screen and they were individual pixel colours, none of those big, square, 8x8 Character Square things that made the speccy really -er- stand out!
*goes all dreamy*

im not gona be forgetting BBC Basic- i still have at least 3 beebs in the attic, and i just reinvented its sound system :P