我有一個字節數組,我想將它重新解釋爲一個可變結構數組,理想情況下不需要複製。使用不安全的代碼很好。我知道字節的數量,以及我最終想要獲得的結構數量。將字節數組重新解釋爲一個結構數組
public struct MyStruct
{
public uint val1;
public uint val2;
// yadda yadda yadda....
}
byte[] structBytes = reader.ReadBytes(byteNum);
MyStruct[] structs;
fixed (byte* bytes = structBytes)
{
structs = // .. what goes here?
// the following doesn't work, presumably because
// it doesnt know how many MyStructs there are...:
// structs = (MyStruct[])bytes;
}
我相信你可以在http://stackoverflow.com/questions/621493/c-unsafe-value-type-找到答案包含轉換技術的數組到字節數組轉換,這些轉換技術適用於您的情況。 – sisve 2010-09-15 11:38:35