Byte order with a large array of characters in C

前端 未结 6 1802
梦毁少年i
梦毁少年i 2021-01-05 01:24

I am doing some socket programming in C, and trying to wrestle with byte order problems. My request (send) is fine but when I receive data my bytes are all out of order. I s

6条回答
  •  [愿得一人]
    2021-01-05 01:55

    This works ok when I'm treating the data as a short, however if I cast the pointer to a char again the bytes are reversed.

    That's what I'd expect.

    What am I doing wrong?

    You have to know what the sender sent: know whether the data is bytes (which don't need reversing), or shorts or longs (which do).

    Google for tutorials associated with the ntohs, htons, and htons APIs.

提交回复
热议问题