Convert byte array to int

前端 未结 4 1119
既然无缘
既然无缘 2021-01-04 18:03

I am trying to do some conversion in C#, and I am not sure how to do this:

private int byteArray2Int(byte[] bytes)
{
    // bytes = new byte[] {0x01, 0x03, 0         


        
4条回答
  •  难免孤独
    2021-01-04 18:33

    byte[] bytes = { 0, 0, 0, 25 };
    
    // If the system architecture is little-endian (that is, little end first), 
    // reverse the byte array. 
    if (BitConverter.IsLittleEndian)
      Array.Reverse(bytes);
    
    int i = BitConverter.ToInt32(bytes, 0);
    Console.WriteLine("int: {0}", i);
    

提交回复
热议问题