Peek and poke *unsigned* long values
Posted: Sat Jun 09, 2012 11:43 am
Works also with PB 5.20
PureBasic has the built-in commands PeekL() and PokeL(), which handle 4 byte signed integers. Here are two small additional procedures, that allow to peek and poke 4 byte unsigned integers.
Regards, Little John
PureBasic has the built-in commands PeekL() and PokeL(), which handle 4 byte signed integers. Here are two small additional procedures, that allow to peek and poke 4 byte unsigned integers.
Regards, Little John
Code: Select all
Procedure.q Peek4u (*addr.Long)
;-- Reads 4 bytes from the specified memory address,
; and returns the value as *unsigned* integer
; (minimum = 0, maximum = 4294967295).
If *addr\l < 0
ProcedureReturn *addr\l + $100000000
Else
ProcedureReturn *addr\l
EndIf
EndProcedure
Procedure Poke4u (*addr.Long, number.q)
;-- Writes an *unsigned* integer of 4 bytes size
; to the specified memory address.
If number >= 0 And number <= $FFFFFFFF
If number > $7FFFFFFF
*addr\l = number - $100000000
Else
*addr\l = number
EndIf
EndIf
EndProcedure
;-- Demo
Define x.l
Debug "Peek and poke unsigned long values:"
Debug ""
Debug "-- Peek"
x = -2147483648 ; minimal Long value
Debug PeekL(@x)
Debug Peek4u(@x)
x = -1
Debug PeekL(@x)
Debug Peek4u(@x)
x = 2147483647 ; maximal Long value
Debug PeekL(@x)
Debug Peek4u(@x)
Debug ""
Debug "-- Poke"
PokeL(@x, -2147483648)
Debug x
Poke4u(@x, 2147483648)
Debug x
PokeL(@x, -1)
Debug x
Poke4u(@x, 4294967295)
Debug x
PokeL(@x, 2147483647)
Debug x
Poke4u(@x, 2147483647)
Debug x