D
David Kyle
Just wondering if there's a quick way to convert types. I quite often have
things like an int that i want to convert the type to a byte array of length
4.
I guess the only way to do it is using unsafe code but even when doing that
I still have to copy the information to the array.
//this is what I would like to make work
int myInt = 244;
byte[] intAsBytes;
unsafe {
intAsBytes = &myInt;
}
//this is what I have to do
int myInt = 1;
byte[] intAsBytes = new byte[4];
unsafe {
byte* pointer = (byte*)&myInt;
for (int i = 0; i < 4; i++)
intAsBytes = *pointer++;
}
things like an int that i want to convert the type to a byte array of length
4.
I guess the only way to do it is using unsafe code but even when doing that
I still have to copy the information to the array.
//this is what I would like to make work
int myInt = 244;
byte[] intAsBytes;
unsafe {
intAsBytes = &myInt;
}
//this is what I have to do
int myInt = 1;
byte[] intAsBytes = new byte[4];
unsafe {
byte* pointer = (byte*)&myInt;
for (int i = 0; i < 4; i++)
intAsBytes = *pointer++;
}