PDA

View Full Version : cast convert / varptr / peek



largo_winch
27-09-2012, 10:47
last not least, I have finished this little "cast" convertion from freebasic. it's only the byte ptr problem ( chapter b) ) here to solve. the second result should be -128 -128, the first part is correct. any help for the part b) is welcome here again. the original code from freebasic is below thinbasic example. bye, largo



Uses "console"

Function TBMain () As Long

Dim i As Integer, ip As Integer 'Ptr
Dim b As Byte, bp As Byte 'Ptr

'a) ------------------------------
i = &h0080 '128

'b = CAST(BYTE, i) ''freebasic
b=(Peek(Byte,VarPtr(i)))

'ip = @i ''freebasic
ip=VarPtr(i)

'b) ---------------------------------
bp=VarPtr(b) ' my idea: add this one ?

bp=(Peek(Byte,VarPtr(ip))) 'BYTE PTR not possible
'bp = CAST(BYTE PTR, ip) ''freebasic
'------------------------------------

' a)
MsgBox 0, "i+b"+Str$(i)+Str$(b) ' correct

' b)
MsgBox 0, "ip+bp"+Str$(VarPtr(ip))+Str$(VarPtr(bp)) ' not correct

' correct result from freebasic looks like:
'128 -128
'128 -128

End Function
'
' freebasic original code example:
'
' DIM i AS INTEGER, ip AS INTEGER PTR
' DIM b AS BYTE, bp AS BYTE PTR

' i = &h0080
' b = CAST(BYTE, i)

' ip = @i
' bp = CAST(BYTE PTR, ip)

' PRINT i, b
' PRINT *ip, *bp
' Sleep

'result
'128 -128
'128 -128

ErosOlmi
27-09-2012, 13:01
Largo,

casting is a method normally used by programming languages using very strict data checking.
In those languages is the programmer that has to force (to cast) a variable type into another.

In thinBasic or other Basic casting is implicit and performed by the language automatically.
The programmer must assume the risk to loose data when assigning variables having higher precision or ranges into variables having lower precision or ranges. For example assigning a LONG to an INTEGER or a QUAD to a LONG or a EXT to a DOUBLE, ... Are all risky operations


So (if I'm not missing something) you code in thinBasic is just:


Dim i As Integer
Dim b As Byte

i = &h0080
b = i

Petr Schreiber
27-09-2012, 17:57
As Eros wrote, you don't need to cast to assign value in thinBASIC. But I think you ... can, if you want:


Long count = 3

Single howMany = 5 + ( count * 5.5 ) As Long

MsgBox 0, howMany


I must admit I didn't use this feature since ThinBASIC Journal #2 :P, but it could be of use in some cases.


Petr

largo_winch
28-09-2012, 11:39
thank you both for replies :) there were several functions with Function="cast"(...) in freebasic example I tried to convert and some other stupid things like "allocstring" commands. I will proof my example for alternative ways or delete all cast's here in my fb examples if that's possible. bye, largo