Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C和x2B之间交换固定浮点数组+;通过套接字和python 我试图用C++发送固定长度的浮点数组 并使用python来获取它 这里是我的C++客户端,SCONNE是一个socket对象< /p> float news[2]; news[0] = 1.2; news[1] = 2.56; char const * p = reinterpret_cast<char const *>(news); std::string s(p, p + sizeof news); send(sConnect, &s[0], sizeof(news), 0);_Python_C++_Sockets - Fatal编程技术网

在C和x2B之间交换固定浮点数组+;通过套接字和python 我试图用C++发送固定长度的浮点数组 并使用python来获取它 这里是我的C++客户端,SCONNE是一个socket对象< /p> float news[2]; news[0] = 1.2; news[1] = 2.56; char const * p = reinterpret_cast<char const *>(news); std::string s(p, p + sizeof news); send(sConnect, &s[0], sizeof(news), 0);

在C和x2B之间交换固定浮点数组+;通过套接字和python 我试图用C++发送固定长度的浮点数组 并使用python来获取它 这里是我的C++客户端,SCONNE是一个socket对象< /p> float news[2]; news[0] = 1.2; news[1] = 2.56; char const * p = reinterpret_cast<char const *>(news); std::string s(p, p + sizeof news); send(sConnect, &s[0], sizeof(news), 0);,python,c++,sockets,Python,C++,Sockets,我在python上得到的数据如下 b'\x9a\x99\x99?\n\xd7#@' 如何将其恢复到我发送的原始浮点数据? 还是有更好的方法来发送和接收数据?您可以使用struct模块来解压缩字节数据 import struct news = struct.unpack('ff', b'\x9a\x99\x99?\n\xd7#@') print(news) 使用您的代码: import struct import socketserver class MyTCPHandler(socketse

我在python上得到的数据如下

b'\x9a\x99\x99?\n\xd7#@'

如何将其恢复到我发送的原始浮点数据?
还是有更好的方法来发送和接收数据?

您可以使用struct模块来解压缩字节数据

import struct
news = struct.unpack('ff', b'\x9a\x99\x99?\n\xd7#@')
print(news)
使用您的代码:

import struct
import socketserver

class MyTCPHandler(socketserver.BaseRequestHandler):
    def handle(self):
        self.data = self.request.recv(8).strip()
        news = struct.unpack('ff', self.data)
        print(news)
struct.unpack“ff”的第一个参数是指定数据预期布局的格式字符串,在本例中为2个浮点。有关其他格式字符,请参见

import struct
import socketserver

class MyTCPHandler(socketserver.BaseRequestHandler):
    def handle(self):
        self.data = self.request.recv(8).strip()
        news = struct.unpack('ff', self.data)
        print(news)