是否有任何python数据类型等效于C中的char数据类型? 在C中,char变量只能存储1个字节的信息。例如C代码及其输出
void main()
{
char a;
printf("Enter the value");
scanf("%d",&a);
printf("A = %d",a);
}
输出
Enter the value 255 A = 255 Enter the value 256 A = 0
我想要python中的确切输出。请帮我。提前谢谢。
答案 0 :(得分:3)
鉴于您的用例是您想要模块化行为,只需将您的输入作为int,并执行a%256
以找到您想要的结果。
答案 1 :(得分:2)
示例:
import struct
import socket
s = socket.socket()
s.connect("127.0.0.1",5000) # connect to some TCP based network (and ignore any sort of endiness issues, which could be solved with other arguments to struct.pack)
data1 = 100
data2 = 200
data_packed = struct.pack("cc",chr(data1),chr(data2)) # pack 2 numbers as bytes (chars)
s.send(data_packed) # put it on the network as raw bytes
data_packed_out = s.recv(100) # get raw bytes from the network
data3,data4 = struct.unpack("cc",data_packed_out) # unpack said bytes
答案 2 :(得分:0)
ctypes是python与... c-types一起工作的库。与使用c。
的设备通信时很有用