Byte order with a large array of characters in C

前端 未结 6 1817
梦毁少年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 02:09

    Apart from your original question (which I think was already answered), you should have a look at your malloc statement. malloc allocates bytes and an unsigned short is most likely to be two bytes.

    Your statement should look like:

    unsigned short *ptr = (unsigned short*) malloc(total * sizeof(unsigned short));
    

提交回复
热议问题