Convert byte array to int

前端 未结 4 1117
既然无缘
既然无缘 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:57

    A fast and simple way of doing this is just to copy the bytes to an integer using Buffer.BlockCopy:

    UInt32[] pos = new UInt32[1];
    byte[] stack = ...
    Buffer.BlockCopy(stack, 0, pos, 0, 4);
    

    This has the added benefit of being able to parse numerous integers into an array just by manipulating offsets..

提交回复
热议问题