Convert 2 bytes into an integer

后端 未结 4 833
挽巷
挽巷 2021-02-12 19:00

I receive a port number as 2 bytes (least significant byte first) and I want to convert it into an integer so that I can work with it. I\'ve made this:

char buf[         


        
4条回答
  •  無奈伤痛
    2021-02-12 19:57

    I receive a port number as 2 bytes (least significant byte first)

    You can then do this:

      int number = buf[0] | buf[1] << 8;
    

提交回复
热议问题