Is Bolt little endian or big endian?

SPFSPF
edited February 21 in BOLT Engine
I'm writing my own serialization/deserialization for streaming. I use BitConverter to convert float, int, etc., and System.Text.Encoding to convert string. It appeared to me that endianness should be my concern. Is Bolt little endian or big endian?

Or since I'm writing it myself, I can choose whichever I use, as long as keeping it consistent on both ends?

Related discussion:
https://stackoverflow.com/questions/66300383/should-i-check-endianness-when-using-system-text-encoding-unicode-getbytesastri

Comments

  • I tried my codes in both ways, only little endian works. So does that mean Bolt uses little endian?
  • Hi @SPF ,

    Bolt does not impose one or another.

    As you are building your own serialization protocol, it's up to you to select the one you will be using.

    If you are serializing by yourself, you just need to check if the current machine you are running the code is Little Endian or not. If it's just, just reverses the byte array when sending and when receiving:
    byte[] data = new byte[100];
    
    if (BitConverter.IsLittleEndian)
    {
        Array.Reverse(data);
    }
    

    Or since I'm writing it myself, I can choose whichever I use, as long as keeping it consistent on both ends?

    Yes.

    --
    Ramon Melo
    Photon Bolt Team
  • @ramonmelo Thank you, that's exactly what I wrote as your example showed. However I don't know why it did not work, even the first int in the stream of bytes is not recovered right. But as I change my code to use little endian, all worked well. Do you have any suggestions what might be the problem?

    To be more precise, my final solutions are:

    For int, float, etc. I use:
    byte[] data = new byte[100];
    
    if (!BitConverter.IsLittleEndian) // notice I added the not sign "!"
    {
        Array.Reverse(data);
    }
    

    For string, I use
    var bytes = System.Text.Encoding.Unicode.GetBytes(v);
    
    instead of
    var bytes = System.Text.Encoding.BigEndianUnicode.GetBytes(v);
    
    . (The former API uses little endian)
Sign In or Register to comment.