Val

Blitz3D Forums/Blitz3D Programming/Val

Barliesque(Posted 2004) [#1]
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???


Rottbott(Posted 2004) [#2]
Use Int()


Genexi2(Posted 2004) [#3]
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.


Barliesque(Posted 2004) [#4]
Tried it. Doesn't compile either.


Sunteam Software(Posted 2004) [#5]
What version of Blitz3d are you using? I've tested with v1.88 and it's fine!


Barliesque(Posted 2004) [#6]
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?


fredborg(Posted 2004) [#7]
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.


Barliesque(Posted 2004) [#8]
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.


Barliesque(Posted 2004) [#9]
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.


TomToad(Posted 2004) [#10]
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


Barliesque(Posted 2004) [#11]
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??


BlackD(Posted 2004) [#12]
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)


Barliesque(Posted 2004) [#13]
LOL!

My thoughts precisely.

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


Neochrome(Posted 2004) [#14]
use 0x00377 for Octal, that would work


Barliesque(Posted 2004) [#15]
That would be interpreted as Hex, for a completely different result.


Damien Sturdy(Posted 2004) [#16]
@TomToad.... err, ive never come across those in BASIC, since the BBC. what versions have you used?


Neochrome(Posted 2004) [#17]
i have a friend who lives on BBC basic, you might as well forget the BBC basic stuff! todays basic is a bit different


aab(Posted 2004) [#18]
'%' for binary? you learn something new every day... I accidently figured out '#' for Hex one day. Twas a long day..


_PJ_(Posted 2004) [#19]
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*


Damien Sturdy(Posted 2004) [#20]
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