Convert int endian

About Monkey 2 Forums Monkey 2 Programming Help Convert int endian

This topic contains 3 replies, has 3 voices, and was last updated by  Hezkore 1 year, 10 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #8245

    Hezkore
    Participant

    I’m trying to read some older file formats, but I need to be able convert my integers (which I get via PeekInt from a DataBuffer) from Big Endian to Little Endian and vice versa.

    Are there any functions for this?

    I’m so bad at searching the docs, I can’t even find a function for displaying values as binary.
    BlitzMax had Bin() I believe.

    #8246

    Mark Sibly
    Keymaster

    DataBuffer has a ByteOrder property, which can even be set from the ctor, eg:

    Kind of untested as I’ve never used them in real code yet. I will probably add some general purpose endian functions to byteorder.monkey2 one of these days too..

    I can’t even find a function for displaying values as binary.

    Try ULongToString and StringToULong. I will probably add Hex, Bin etc on day too as I find these hard to rember myself.

    #8253

    TomToad
    Participant

    A class for swapping big/little endian from/to native format.  Just create an instance of the class and call endian.swapEndian(n:int, original:Int) where n is the number to convert and original is the byte order you are converting from/to.  Nice thing is, the same method is used in each case. pass the int you wish to save followed by the ByteOrder value you are saving to, or pass the value you just read followed by the stored  ByteOrder .

    #8254

    Hezkore
    Participant

    Wow TomToad, you went full on with that!

    Big thanks everyone.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.