Byte types

Monkey Forums/Monkey Programming/Byte types

Richard Betson(Posted 2014) [#1]
Hi,

Monkey as i understand it does not support byte types and this is so to stay target compatible. Are byte types still not supported?

The reason I ask is I use them in my networking code for my Blitz Max game and will have to change things round a bit to support integers where bytes are used. Right now I capture keyboard sequences as bytes and would have to shift this to integers which gets costly for the server with a few hundred clients being supported.

No biggie just getting up to speed. ;)

Thanks,


computercoder(Posted 2014) [#2]
Thats right. You can see a list of all the supported types in the documentation as well - Select the "Language" link, then click the "Types" link. :)

Here's a quick list of what is there:
Boolean
Integer
Floating point
String
Array
Object


AdamRedwoods(Posted 2014) [#3]
another way to use byte types is to use DataBuffers:
http://monkeycoder.co.nz/docs/html/Modules_brl.databuffer_DataBuffer.html

i think every target supports databuffers. it's not direct access so the implementation is a bit slower (verrrry slow on android, java's fault).
otherwise, bitshifting and masking ints is probably what you'll need to do.


John Galt(Posted 2014) [#4]
It's a shame there's not a 'desktop only' mode that supports more of the stuff Blitzmax did.


Richard Betson(Posted 2014) [#5]
^ It is, but understandable. Marks goal has been to resist this and I think and he is really right about it.

Really I was checking in to see if this issue had changed. I have been so busy writing Phoenix USC that I am now just getting to the point of looking at a Money port. The last time I really used Monkey was during beta testing and since then a lot has improved. It looks as if UDP networking is getting in place and that's a must for me.

Small things like byte types are missed here in Monkey and other features as well. But if there is one thing I am good at it's finding cleaver ways to get around limitations. ;)

- Rich -


John Galt(Posted 2014) [#6]
I half agree with you, Rich. I can certainly understand why Mark made that decision. Thing is, I like to write as much reusable code as possible and right now there's always the conundrum of whether to start something in Monkey or Max.

I have been so busy writing Phoenix USC that I am now just getting to the point of looking at a Money port
Ha! Freudian slip? ;)


Richard Betson(Posted 2014) [#7]
John,

"Ha! Freudian slip? ;) "
LOL.. more like I need to put on my dam glasses. Twas funny though. :)

I feel ya. I have written my project with the goal of porting it to Monkey from the start. It is possible to make it easier if you avoid using Bmax specific commands and I have for example taken steps to stay as port friendly as possible really thinning the commands I use and maintaining a structure that ports as well as possible. Even going to the extent of writing my own window based GUI so that I can port it to Monkey. Not an easy task for sure but worth it and if you stick to using commands that cross over to Monkey very doable. A good example of this is the Bananas example simple_vertlet_physics (under my name) which is a straight up port of grable's Blitz Max code (see code archives) and I added it as an example of how easy it can be to port over Blitz Max code. That took less then a day to port over and get running. It really is a matter of resisting the use of Blitz Max specific commands and structure.

I'll let you know how it goes for me as I will try to port over my GUI code first which is a really good test of how well I've done in making my Blitz Max project Monkey friendly.


John Galt(Posted 2014) [#8]
Thanks, Rich. Some interesting insight there to mull over on keeping things 'portable'. I'm glad to here porting is not too arduous if you play it right. I've been watching Phoenix with interest as I like what you've been doing with it. Now I have yet another reason to keep an eye one it!

MonkeyMax looks like a nice option for porting the other way although development seems to have stalled.
http://www.monkeycoder.co.nz/Community/posts.php?topic=1647


EdzUp(Posted 2014) [#9]
I have never understood why it was never in there tbh, c has char as a byte value (unsigned char for 0-255), iOS has the can as obj c is basically just c with extra stuff. Java has byte too ( http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html ) so why is it not in there. It would allow loads of us to do simple file and network stuff.


Karja(Posted 2014) [#10]
MonkeyMax is still being updated now and then. I used it to release my latest game, so I add features I need and keep it compatible with the latest Monkey releases whenever I upgrade my Monkey installation. Make sure you get it from the Google Code site though: https://code.google.com/p/monkey-max/wiki/MonkeyMax


Samah(Posted 2014) [#11]
Note that all numeric types in Java are signed, so you would need to use char, short or int. This is why InputStream.read() returns an int instead of a byte. Also char is two bytes in Java to support wide characters.


EdzUp(Posted 2014) [#12]
Is there a way to read the bytes of a file (as bytes not ints) and do adjustments on them and get the 0-255 value out. As an example read in a value say character A (ASCII 65) take say 799 from it and get a 0-255 out ie it wraps to byte values?

With monkey having ints everywhere it does cause a few issues with file access areas.


Danilo(Posted 2014) [#13]
Is there a way to read the bytes of a file (as bytes not ints) and do adjustments on them and get the 0-255 value out. As an example read in a value say character A (ASCII 65) take say 799 from it and get a 0-255 out ie it wraps to byte values?

As a start:



EdzUp(Posted 2014) [#14]
Thanks will take a look


Danilo(Posted 2014) [#15]
Display all (unsigned) Bytes in a file:
Import brl.databuffer

Function Main()
    Local buffer := DataBuffer.Load("/Users/danilo/test.cs")
    If buffer
        For Local i := 0 Until buffer.Length()
            Local byte:Int = buffer.PeekByte(i) & $FF
            Print( byte + " - " + String.FromChar(byte) )
        next
    Endif
End



EdzUp(Posted 2014) [#16]
So from what i can gather its the & $FF on the code to get the byte value


Danilo(Posted 2014) [#17]
Yes, it is used to mask out the lowest 8 bits of the int. To convert a 16bit Short, you would take $FFFF.
You can take binary numbers if you want:
Function Main()

    Local byte1:Int = 250
    For Local i:=0 To 10
        Print( byte1 & $FF )                ' using hex $FF
        byte1 += 1
    Next

    Print("----------")

    Local byte2:Int = 250
    For Local i:=0 To 10
        Print( byte2 & %11111111 )          ' using 8 binary bits
        byte2 += 1
    Next
    
    Print("----------")

    Local short1:Int = $FFFA
        For Local i:=0 To 10
        Print( short1 & $FFFF )             ' using hex $FFFF
        short1 += 1
    next

    Print("----------")

    Local short2:Int = $FFFA
        For Local i:=0 To 10
        Print( short2 & %1111111111111111 ) ' using 16 binary bits
        short2 += 1
    next

End

It is the BITWISE AND ('&') operation with 8 or 16 lowest bits, so all other (higher) bits get removed.