DataBuffers PeekByte() returns negative values

Monkey Forums/Monkey Bug Reports/DataBuffers PeekByte() returns negative values

Pharmhaus(Posted 2014) [#1]
Hi mark,

don't know if this is a bug but this returns negative numbers:
Strict
Import brl.databuffer

Function Main:Int()

	Local db:DataBuffer = New DataBuffer(1024)
	db.PokeByte(1, 252)
	Local i:int = db.PeekByte(1)
	Print i
	Return 0
End

If this is by-design, would you consider providing a ReadUByte() Method for the databuffer?


Pharmhaus(Posted 2014) [#2]
Sorry, I though it was already fixed but I run the wrong test case, this still exists in 81b


Danilo(Posted 2014) [#3]
You can easily read it as unsigned data by using a 'binary And' operation to mask out the relevant bits:
Strict
Import brl.databuffer

Function Main:Int()

    Local db:DataBuffer = New DataBuffer(1024)

    Print "Byte:"
    db.PokeByte(1, 252)
    'db.PokeByte(1, -4)
    
    Print db.PeekByte(1)          ' Read  signed  Byte
    Print db.PeekByte(1) & $FF    ' Read unsigned Byte

    Print "Word/Short:"
    db.PokeShort(1, 65500)
    'db.PokeShort(1, -36)
    
    Print db.PeekShort(1)         ' Read  signed  Word
    Print db.PeekShort(1) & $FFFF ' Read unsigned Word

    Return 0
End

It is good to have both ways. Read data and interpret it as signed and unsigned data.


Pharmhaus(Posted 2014) [#4]
Well, If you tell me this is by design, could this at least be stated in the docs that the returned value is signed (maybe in bold)?
Wouldn't have changed anything as I simply ported good ol' bmax code and I would have fallen for the same trap because all I did was copy&paste.
The bit twiddling is not a problem the behaviour was simply extremly unexpected at the very moment I ported the code.


skid(Posted 2014) [#5]
All monkey integers are signed, there is no precedent anywhere for the use of unsigned in any of it's modules that I am aware of.

Which particular blitzmax command are you talking about ph?


Pharmhaus(Posted 2014) [#6]
Try:
SuperStrict

Local bank:TBank = CreateBank(1024)
bank.PokeByte(1, 252)

Print bank.PeekByte( 1)


And compare it with the Monkey X version.
This would be no problem for a few lines but if you try to port whole modules over things get ugly pretty fast when multiple things now work completely different.
Debugging old code n' stuff....


skid(Posted 2014) [#7]
I concur, docs need updating as byte is specifically signed in bmx and undefined in monkey.


marksibly(Posted 2014) [#8]
The reason it works like this is mainly because that's the way most targets natively implement the databuffer methods - ie: it's pretty much optimal.

Adding PeekUByte etc wouldn't be too much of a hassle though - will think about it.