【发布时间】:2014-11-12 20:26:53
【问题描述】:
我在 (windows) 中有一个以二进制格式发送日志的应用程序。 将其转换为字符串的 C# 代码是:
public static CounterSampleCollection Deserialize(BinaryReader binaryReader)
{
string name = binaryReader.ReadString(); // counter name
short valueCount = binaryReader.ReadInt16(); // number of counter values
var sampleCollection = new CounterSampleCollection(name);
for (int i = 0; i < valueCount; i++)
{
// each counter value consists of a timestamp + the actual value
long binaryTimeStamp = binaryReader.ReadInt64();
DateTime timeStamp = DateTime.FromBinary(binaryTimeStamp);
float value = binaryReader.ReadSingle();
sampleCollection.Add(new CounterSample(timeStamp, value));
}
return sampleCollection;
}
我有一个正在监听端口的 python udp 套接字,但不知道如何将接收到的二进制数据转换为字符串,以便进一步解析它。
请任何python专家帮我把那个函数转换成python函数,这样我就可以把我收到的数据转换成python。
到目前为止我的代码:
import socket
UDP_IP = "0.0.0.0"
UDP_PORT = 40001
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(8192) # buffer size is 8192 bytes
print "[+] : ", data
// this prints the binary
// convert the data to strings ??
【问题讨论】:
标签: c# python serialization binary