Peek and poke *unsigned* long values

Share your advanced PureBasic knowledge/code with the community.
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Peek and poke *unsigned* long values

Post by Little John »

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

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
Last edited by Little John on Mon Aug 19, 2013 4:39 pm, edited 1 time in total.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Peek and poke *unsigned* long values

Post by IdeasVacuum »

Thanks for sharing :mrgreen:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply