Results 1 to 7 of 7

Thread: Which is better ? Endianness question.

  1. #1
    Developer RyBack's Avatar
    Join Date
    Apr 2014
    Location
    In Front of the screen
    Posts
    1,594

    Default Which is better ? Endianness question.

    Eh so I have the server to send the short as big endian.
    Are both of those expressions right ?
    Someone correct me this endian thing got me headaches.

    Click image for larger version. 

Name:	endian.JPG 
Views:	65 
Size:	13.7 KB 
ID:	1830
    Last edited by RyBack; August 13th, 2017 at 11:51 PM.
    Discord: RyBack#4239

  2. #2
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,173

    Default

    I don't follow? Both pictures are the same. What are you trying to ask?

  3. #3
    Developer RyBack's Avatar
    Join Date
    Apr 2014
    Location
    In Front of the screen
    Posts
    1,594

    Default

    The server is sending the short as big endian, the client does ntohs(),
    Now for compatibility , is it better to use ntohs() or (resp[0] << 8) | (resp[1] & 0xff).
    Or it wouldn't matter ?
    Note: resp is an std::string and the first two bytes in it are the sent-from-server-as-big-endian short value.
    Discord: RyBack#4239

  4. #4
    Über Prodigy & Developer Razo[R]apiD's Avatar
    Join Date
    May 2010
    Location
    Poland, Lublin
    Posts
    3,256

    Default

    You should always use ntohs and other functions from the family if you want your code to be compatible across different hardware architectures.

    Network Byte Order is equal to Big Endian, but for example x86 architecture is Little Endian, so you have to swap order of bytes for them to represent a valid value.
    But there are architectures which are Big Endian, and there is no need for swapping for them.

    You can treat nthos as a macro, which is defined in compiler headers to:

    - on LE arch. swaps bytes
    - on BE do nothing

  5. #5
    Developer RyBack's Avatar
    Join Date
    Apr 2014
    Location
    In Front of the screen
    Posts
    1,594

    Default

    Oh hi rr.
    No pressure you don't have to do anything thing after all you've done for this game.
    Now
    The thing is in my server I must specify the endianess of a number when appending it to a packet.
    Waya think would fit c++/ntohs ?
    Discord: RyBack#4239

  6. #6
    Über Prodigy & Developer Razo[R]apiD's Avatar
    Join Date
    May 2010
    Location
    Poland, Lublin
    Posts
    3,256

    Default

    If you never plan to run your application on big endian processor then don't even bother about all of that and send/recv everything in little endian. just read/write the value without any conversion.

  7. #7
    Developer RyBack's Avatar
    Join Date
    Apr 2014
    Location
    In Front of the screen
    Posts
    1,594

    Default

    My client platform is x86/x64 windows. I don't compile for x64 but I know x86 code works on x64.
    What I meant is : https://nodejs.org/api/buffer.html
    I must specify the endianess of any int larger than a byte.
    Discord: RyBack#4239

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •