Using a file to convert back and forth is a waste. You can simply use a pointer to your struct data to do the transfer. But bear in mind that the way you wrote may cause issue mainly due to two reasons: 1. Compiler can do some padding to struct 2. Endianess issue (big/little endian) So in order to make it correct during network transmission, you first want to make sure that compiler padding is removed, so you can use #pramga directive: #pragma pack(1) struct packed_struct { unsigned int f1:8; unsigned int f2:5; unsigned int f3:3; } pack; Fortunately, your bit fields do not break in between one byte alignment. So using the above technique, your bytes won't be garbled. And you can make sure that sizeof(pack)=2. Then in order to make it work for all CPU architectures (big/little endian), you need to use htons() and ntohs() functions. You can refer to the following manpage: http://pubs.opengroup.org/stage7tc1/functions/htonl.html Then finally you want to put all your struct into a char[2] array and then fire it and let it go!
ADBC,非常感谢指点。 实际数据中有很多跨字节、破字节的数值,字节序(endianess)的问题我已 经遇到了。还好我们的数据不是向公众传递,自己内部几台机器做相应的调整就好了。 “You can simply use a pointer to your struct data to do the transfer.” 我总觉得存盘的步骤可以省略。可是我试了几次都不行,劳驾您再说清楚一些。我 试了这个,编译通不过: char * char_pointer; char_pointer = &packed_data; 指针怎么用,我不太懂。
【在 d****i 的大作中提到】 : Using a file to convert back and forth is a waste. You can simply use a : pointer to your struct data to do the transfer. But bear in mind that the : way you wrote may cause issue mainly due to two reasons: : 1. Compiler can do some padding to struct : 2. Endianess issue (big/little endian) : So in order to make it correct during network transmission, you first want : to make sure that compiler padding is removed, so you can use #pramga : directive: : #pragma pack(1) : struct packed_struct {
d*i
10 楼
You need to create a unsigned char[2] array, and then copy the data from struct to char array, and then invokw the system call send() to send bytes. See: http://pubs.opengroup.org/onlinepubs/7908799/xns/send.html I think actually you don't need to use htons since there are no fields larger than 1 byte in your struct.
【在 r*****8 的大作中提到】 : ADBC,非常感谢指点。 : 实际数据中有很多跨字节、破字节的数值,字节序(endianess)的问题我已 : 经遇到了。还好我们的数据不是向公众传递,自己内部几台机器做相应的调整就好了。 : “You can simply use a pointer to your struct data to do the transfer.” : 我总觉得存盘的步骤可以省略。可是我试了几次都不行,劳驾您再说清楚一些。我 : 试了这个,编译通不过: : char * char_pointer; : char_pointer = &packed_data; : 指针怎么用,我不太懂。 :
r*8
11 楼
“You need to create a unsigned char[2] array, and then copy the data from struct to char array,” 这两句编译通不过: unsigned char char_array[2]; strcpy(char_array, packed_data); 还有,不能strcpy,因为真实数据中间可能有0x0字符,strcpy 到了0x0字符就 不往下走了。 要读出每一个位置来,如果是指针,这也是不好办的。 发送我有经验,不是问题。
.
【在 d****i 的大作中提到】 : You need to create a unsigned char[2] array, and then copy the data from : struct to char array, and then invokw the system call send() to send bytes. : See: : http://pubs.opengroup.org/onlinepubs/7908799/xns/send.html : I think actually you don't need to use htons since there are no fields : larger than 1 byte in your struct.
Don't use strcpy, it is used to copy string only. Use memcpy, or use direct assign: char_array[0] = packed_data.f1; char_array[1] = packed_data.f2 << 3 + packed_data.f3;
【在 r*****8 的大作中提到】 : “You need to create a unsigned char[2] array, and then copy the data from : struct to char array,” : 这两句编译通不过: : unsigned char char_array[2]; : strcpy(char_array, packed_data); : 还有,不能strcpy,因为真实数据中间可能有0x0字符,strcpy 到了0x0字符就 : 不往下走了。 : 要读出每一个位置来,如果是指针,这也是不好办的。 : 发送我有经验,不是问题。 :