私は自分で解決することができました。 次のことを想定します
今
MSG_STRUCT_TYPE OutputMsg; // e.g.750 bytes
int startByte = 28; // the start byte of floating point field in the message
float defVal = -1.75f; // the default value for the field above
、我々は次のような操作を行うことができますreinterpret_castはを使用して:
unsigned char* bytes_to_set = reinterpret_cast<unsigned char*>(&defVal);
unsigned char* msgBuff = reinterpret_cast<unsigned char*>(&OutputMsg);
*reinterpret_cast<float*>(&msgBuf[startByte]) = *reinterpret_cast<float*>(bytes_to_set);
仕事をすること。
実装し、njuffaのコメント怒鳴るを確認した後、私は答え別(simplierとクリーナー)としてそれを追加している:
unsigned char* msgBuff = reinterpret_cast<unsigned char*>(&OutputMsg);
memcpy(&msgBuf[startByte], &defVal, sizeof(float);
それが依存、何をすべき浮動のデフォルト値ポイントメンバーは?ゼロの場合、ゼロはすべて正常です。 –
しかし、もしあなたがC++でプログラミングしているのであれば、それ自身のメンバを適切に初期化する構造体ごとにデフォルトのコンストラクタを持つことができないのですか?非仮想メンバ関数(コンストラクタなど)は、構造体のデータを「汚染」しません。 –
デフォルト値は遅れる可能性があります。記述ファイルは、第三者から私に渡されます。 – rainbringer