クラスSystem.BitConverter
検討このクラス等のInt32、Int64の、ダブル、ブール値として指定されたインデックスから開始し、バックこれらのタイプのバイトシーケンスにバイトを再解釈する機能を有します。
例:
int32 x = 0x12345678;
var xBytes = BitConverter.GetBytes(x);
// bytes is a byte array with length 4: 0x78; 0x56; 0x34; 0x12
var backToInt32 = BitConverter.ToInt32(xBytes, 0);
またはあなたの配列が混合データが含まれている場合:
double d = 3.1415;
int16 n = 42;
Bool b = true;
Uint64 u = 0xFEDCBA;
// to array of bytes:
var dBytes = BitConverter.GetBytes(d);
var nBytes = BitConverter.GetBytes(n);
var bBytes = BitConverter.GetBytes(b);
var uBytes = BitConterter.GetBytes(u);
Byte[] myBytes = dBytes.Concat(nBytes).Concat(bBytes).Concat(uBytes).ToArray();
// startIndexes in myBytes:
int startIndexD = 0;
int startIndexN = dBytes.Count();
int startIndexB = startIndexN + nBytes.Count();
int startIndexU = startIndexB + bBytes.Count();
// back to original elements
double dRestored = Bitconverter.ToDouble(myBytes, startIndexD);
int16 nRestored = BitConverter.ToInt16(myBytes, startIndexN);
bool bRestored = BitConverter.ToBool(myBytes, startIndexB);
Uint64 uRestored = BitConverter.ToUint64(myBytes, startIndexU);
構造体へのポインタにCでのボイドポインタをキャストような?いいえ。 –
[管理対象構造体へのバイト配列のキャスト]の可能な複製(http://stackoverflow.com/questions/6335153/casting-a-byte-array-to-a-aged-structure)。基本的にストリームに変換して読み込みます。 – MickyD
[C# - バイト配列を構造体の配列にキャストしたり、その逆(逆の場合)]の可能な複製(http://stackoverflow.com/questions/17840552/c-sharp) -cast-a-byte-array-of-struct-and-versa-reverse) –