I have the following problem. I have an array of bytes that I want to convert intro an array of primitive types. But I don’t know the type. (This is given as an array of types). As a result I need an array of objects.
Of course I could use a switch on the types (there are only a limited number of them), but I wonder if there is a better solution for that.
Example:
byte[] byteData = new byte[] {0xa0,0x14,0x72,0xbf,0x72,0x3c,0x21}
Type[] types = new Type[] {typeof(int),typeof(short),typeof(sbyte)};
//some algorithm
object[] primitiveData = {...};
//this array contains an the following elements
//an int converted from 0xa0,0x14,0x72,0xbf
//a short converted from 0x72, 0x3c
//a sbyte converted from 0x21
Is there an algorithm for this or should I use a switch
This code uses unsafe to get a pointer to the byte array buffer, but that shouldn’t be a problem.
[Edit – changed code after comment]