C# convert from uint[] to byte[]

后端 未结 5 1984
[愿得一人]
[愿得一人] 2021-01-13 20:35

This might be a simple one, but I can\'t seem to find an easy way to do it. I need to save an array of 84 uint\'s into an SQL database\'s BINARY field. So I\'m using the fol

相关标签:
5条回答
  • 2021-01-13 20:48
    byte[] byteArray = Array.ConvertAll<uint, byte>(
        uintArray,
        new Converter<uint, byte>(
            delegate(uint u) { return (byte)u; }
        ));
    

    Heed advice from @liho1eye, make sure your uints really fit into bytes, otherwise you're losing data.

    0 讨论(0)
  • 2021-01-13 20:50

    There is no built-in conversion function to do this. Because of the way arrays work, a whole new array will need to be allocated and its values filled-in. You will probably just have to write that yourself. You can use the System.BitConverter.GetBytes(uint) function to do some of the work, and then copy the resulting values into the final byte[].

    Here's a function that will do the conversion in little-endian format:

        private static byte[] ConvertUInt32ArrayToByteArray(uint[] value)
        {
            const int bytesPerUInt32 = 4;
            byte[] result = new byte[value.Length * bytesPerUInt32];
            for (int index = 0; index < value.Length; index++)
            {
                byte[] partialResult = System.BitConverter.GetBytes(value[index]);
                for (int indexTwo = 0; indexTwo < partialResult.Length; indexTwo++)
                    result[index * bytesPerUInt32 + indexTwo] = partialResult[indexTwo];
            }
            return result;
        }
    
    0 讨论(0)
  • 2021-01-13 20:53

    If you need all the bits from each uint, you're gonna to have to make an appropriately sized byte[] and copy each uint into the four bytes it represents.

    Something like this ought to work:

    uint[] uintArray;
    
    //I need to convert from uint[] to byte[]
    byte[] byteArray = new byte[uintArray.Length * sizeof(uint)];
    for (int i = 0; i < uintArray.Length; i++)
    {
        byte[] barray = System.BitConverter.GetBytes(uintArray[i]);
        for (int j = 0; j < barray.Length; j++)
        {
              byteArray[i * sizeof(uint) + j] = barray[j];
        }
    }
    cmd.Parameters.Add("@myBindaryData", SqlDbType.Binary).Value = byteArray;
    
    0 讨论(0)
  • 2021-01-13 21:01

    How about:

    byte[] byteArray = uintArray.SelectMany(BitConverter.GetBytes).ToArray();
    

    This'll do what you want, in little-endian format...

    0 讨论(0)
  • 2021-01-13 21:05

    You can use System.Buffer.BlockCopy to do this:

    byte[] byteArray = new byte[uintArray.Length * 4];
    Buffer.BlockCopy(uintArray, 0, byteArray, 0, uintArray.Length * 4];
    

    http://msdn.microsoft.com/en-us/library/system.buffer.blockcopy.aspx

    This will be much more efficient than using a for loop or some similar construct. It directly copies the bytes from the first array to the second.

    To convert back just do the same thing in reverse.

    0 讨论(0)
提交回复
热议问题