gpt4 book ai didi

c++ - 想要将 char 数组的部分转换/类型转换为值

转载 作者:太空宇宙 更新时间:2023-11-04 12:17:13 25 4
gpt4 key购买 nike

我正在玩弄网络,但在将包含大量数据的数据包转换为我想要的值时遇到了一些障碍。

基本上我已经制作了一个模型包,我希望我的包看起来有点像。本质上是一个 Char(8 位值),指示消息是什么,由 switch 语句检测到,然后根据该 8 位值之后的数据填充值。我希望我的数据包中包含各种可能不按顺序排列的消息。

例如,我可能会在最后看到心跳,或者聊天消息中的一串文本,等等。

我只想对我的程序说,从 char 数组中的某个点获取数据,然后将它们转换为我想要的形式(如果这是它的术语?)。有什么简单的好方法可以做到这一点?

char bufferIncoming[15];
ZeroMemory(bufferIncoming,15);

//Making a mock packet
bufferIncoming[0] = 0x01; //Heartbeat value
bufferIncoming[1] = 0x01; //Heartbeat again just cause I can
bufferIncoming[2] = 0x10; //This should = 16 if its just an 8bit number,

bufferIncoming[3] = 0x00; // This
bufferIncoming[4] = 0x00; // and this
bufferIncoming[5] = 0x00; // and this
bufferIncoming[6] = 0x09; // and this should equal "9" of its is a 32bit number (int)

bufferIncoming[7] = 0x00;
bufferIncoming[8] = 0x00;
bufferIncoming[9] = 0x01;
bufferIncoming[10] = 0x00; //These 4 should be 256 I think when combines into an unsigned int
//End of mockup packet


int bufferSize = 15; //Just an arbitrary value for now
int i = 0;
while (i < bufferSize)
{
switch (bufferIncoming[i])
{
case 0x01: //Heart Beat
{
cout << "Heartbeat ";
}
break;
case 0x10: //Player Data
{
//We've detected the byte that indicates the following 8 bytes will be player data. In this case a X and Y position

playerPosition.X = ??????????; //How do I combine the 4 hex values for this?
playerPosition.Y = ??????????;
}
break;
default:
{
cout << ".";
}
break;

}
i++;
}
cout << " End of Packet\n";


更新

根据 Clairvoire 的想法,我添加了以下内容。

playerPosition.X = long(bufferIncoming[3]) << 24 | long(bufferIncoming[4]) << 16 | long(bufferIncoming[5]) << 8 | long(bufferIncoming[6]);

请注意,我围绕变化的值进行了更改。

另一个重要的变化是

unsigned char bufferIncoming[15]

如果我不这样做,我会得到与每个元素的组合混合的负值。我不知道编译器在幕后做了什么,但这实在是太烦人了。

如您所想,这不是我的首选解决方案,但我会试一试。 “乍得”有一个很好的例子说明我如何构建它,工作中的一位程序员同事也推荐了他的实现。但是……

我觉得必须有一种更快更清洁的方式来做我想做的事情。我试过类似...

playerPosition.X = *(bufferIncoming + 4) //Only giving me the value of the one hex value, not the combined >_<
playerPosition.X = reinterpret_cast<unsigned long>(&bufferIncoming); //Some random number that I dont know what it was

..以及我删除的其他一些也不起作用的东西。我期望做的是指向该字符缓冲区中的某处并说“嘿 playerPosition,从这个位置开始读取,并根据那里的字节数据填写您的值”。

比如也许……

playerPosition = (playerPosition)bufferIncoming[5]; //Reads from this spot and fills in the 8 bytes worth of data
//or
playerPosition.Y = (playerPosition)bufferIncoming[9]; //Reads in the 4 bytes of values

...为什么它不能像那样工作或类似的东西?

最佳答案

这可能有一个漂亮的版本,但我个人会像这样使用左移和 or 组合四个 char 变量:

playerPosition.X = long(buffer[0]) | long(buffer[1])<<8 | long(buffer[2])<<16 | long(buffer[3])<<24;

字节顺序不应该是一个问题,因为按位逻辑总是执行相同的,最低的顺序在右边(就像十进制数字的位置在右边一样)

编辑:字节顺序可能会成为一个因素,这取决于发送机器在通过网络发送整数之前最初如何拆分整数。如果它不以与使用移位重组整数相同的方式分解整数,您可能会得到一个值,其中第一个字节在最后,最后一个字节在前。像这样的小歧义促使大多数人使用网络库,啊哈。

使用按位拆分整数的示例如下所示

buffer[0] = integer&0xFF;
buffer[1] = (integer>>8)&0xFF;
buffer[2] = (integer>>16)&0xFF;
buffer[3] = (integer>>24)&0xFF;

关于c++ - 想要将 char 数组的部分转换/类型转换为值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7136344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com