我一直致力于传统的C++应用程序,我绝对不在我的舒适区域(一件好事).我想知道是否有人会非常友好地给我一些指示(双关语).
我需要将unsigned char数组中的2个字节转换为unsigned short.字节是连续的.
有关我想要做的事情的一个例子:
我从套接字接收一个字符串并将其放在unsigned char数组中.我可以忽略第一个字节,然后接下来的2个字节应转换为unsigned char.这将只在Windows上,因此没有Big/Little Endian问题(我知道).
这就是我现在拥有的(显然不是很明显):
//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])
Run Code Online (Sandbox Code Playgroud)