As you mentioned, you will use uint[] for encryption.
For some reason you copy arrays using BlockCopy . But you want to access byte[] as uint[] without transforming or casting the whole array.
Then something is missing? Is there anything in C # for this?
Think one more hour, I think, for what you need, there should be an indexer .
Here is the code. It's simple.
public partial class UIntIndexer { static int ComputeElementCount(int size, int bytesCount) { var r=~0; return Math.DivRem(bytesCount, size, out r)+(r>0?1:0); } static int ComputeIndex(int index) { return sizeof(uint)*index; } public UIntIndexer(byte[] array) { m_Length=ComputeElementCount(sizeof(uint), (m_Array=array).Length); } public uint this[int index] { set { var startIndex=ComputeIndex(index); var bytes=BitConverter.GetBytes(value); var count=m_Length>1+index?sizeof(uint):m_Array.Length-startIndex; Buffer.BlockCopy(bytes, 0, m_Array, startIndex, count); } get { var startIndex=ComputeIndex(index); if(m_Length>1+index) return BitConverter.ToUInt32(m_Array, startIndex); else { var bytes=new byte[sizeof(uint)]; Buffer.BlockCopy(m_Array, startIndex, bytes, 0, m_Array.Length-startIndex); return BitConverter.ToUInt32(bytes, 0); } } } public int Length { get { return m_Length; } } byte[] m_Array; int m_Length; }
If there is a thread safety issue, you may need to synchronize the source array. Below is the code for testing:
var alldata=new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var block32=new byte[alldata.Length]; var uintIndexer=new UIntIndexer(block32); Buffer.BlockCopy(alldata, 0, block32, 0, alldata.Length); Debug.Print("uintIndexer.Length={0}", uintIndexer.Length); for(var i=uintIndexer.Length; i-->0; ) Debug.Print("uintIndexer[{0}]={1}", i, uintIndexer[i]);