我怎么能得到IP和端口从有效载荷无符号字符,但
How can i get IP and PORT from payload unsigned char buf?
我有unsigned char buf:
IP位于位置197,端口位于位置205。我如何从这个无符号字符中得到正确的IP和端口?
我尝试了一些数字转换,但没有运气:(
我假设,IP从第197个位置开始,它在数组中占据4个位置(每个八分区1个),1个位置用于端口。在C/c++中,第197个位置应该以索引196访问。因此,您可以通过以下方式访问IP和端口地址的4个八分位数,
short int octants[] ={buf[196],buf[197],buf[198],buf[199]} ;
short int portId = buf[204] ;
好吧,我找到了解决方案,谢谢你在正确的方向踢家伙:)
输出正确结果的代码:
printf("IP: %d.%d.%d.%d", buf[197], buf[198], buf[199], buf[200]);
int port = 0; // Start with zero
port |= buf[204] & 0xFF; // Assign first byte to port using bitwise or.
port <<= 8; // Shift the bits left by 8 (so the byte from before is on the correct position)
port |= buf[205] & 0xFF;
printf(" Port: %d", port);
结果为:IP: 8.8.8.8 Port: 53
相关文章:
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 将无符号char*转换为std::istream*C++
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- C++中无符号字符溢出
- 使用无符号字符数组有效存储内存
- 如何打印boost多精度128位无符号整数
- C++模板函数,用于比较任何无符号整数和有符号整数
- 在 std::无符号字符的向量处存储 int 的十六进制表示形式
- 无符号的 int 到有效数字
- 一种将 Dart 中的字节数据转换为 C++ 中的无符号字符*的有效方法?
- 为大无符号整数分配内存的有效方法
- 获取两个无符号整数 C++ 乘积的高 32 位的有效方法
- C/C++:将 char[] 转换为 int 失败,无符号 char[] 到 int 有效,为什么
- 使用无符号数据类型强制执行非负值和/或有效值是否是一种最佳做法
- 在字符串附加函数中,null终止无符号字符缓冲区的有效方法
- 将39567865个无符号字符加载到RAM:有效方法
- 将无符号字符* 作为 byte[] 从 C++ 传递到 java 的最有效方法
- 我怎么能得到IP和端口从有效载荷无符号字符,但