私は従来の C++ アプリケーションに取り組んできましたが、間違いなく自分のコンフォート ゾーンの外にいます (良いことです)。誰かが私にいくつかの指針を与えるほど親切であるかどうか疑問に思っていました(しゃれが意図されています)。
unsigned char 配列の 2 バイトを unsigned short にキャストする必要があります。バイトは連続しています。
私がやろうとしていることの例:
ソケットから文字列を受け取り、unsigned char 配列に配置します。最初のバイトを無視して、次の 2 バイトを unsigned char に変換する必要があります。これは Windows でのみ実行されるため、ビッグ/リトル エンディアンの問題はありません (私が認識しています)。
これが私が今持っているものです(明らかに機能していません):
//packetBuffer is an unsigned char array containing the string "123456789" for testing
//I need to convert bytes 2 and 3 into the short, 2 being the most significant byte
//so I would expect to get 515 (2*256 + 3) instead all the code I have tried gives me
//either errors or 2 (only converting one byte
unsigned short myShort;
myShort = static_cast<unsigned_short>(packetBuffer[1])