Good way to convert between short and bytes?

后端 未结 5 1462
攒了一身酷
攒了一身酷 2021-01-02 09:30

I need to take pairs of bytes in, and output shorts, and take shorts in and output pairs of bytes. Here are the functions i\'ve devised for such a purpose:

s         


        
5条回答
  •  借酒劲吻你
    2021-01-02 10:07

    Use BitConverter

    short number = 42;
    byte[] numberBytes = BitConverter.GetBytes(number);
    short converted = BitConverter.ToInt16(numberBytes);
    

提交回复
热议问题