[英]Storing Unsigned Char as Binary in File with C++?
I have written the following code to print the data in Pin and Pout to a file:我编写了以下代码将 Pin 和 Pout 中的数据打印到文件:
void run() {
while ( in.readable() >= 17*11*12+SIZE_RSPACKET &&
out.writable() >= 1 ) {
u8 *pin = in.rd()+17*11*12, *pend=pin+SIZE_RSPACKET;
u8 *pout= out.wr()->data;
for ( int delay=17*11; pin<pend;
++pin,++pout,delay=(delay-17+17*12)%(17*12) )
*pout = pin[-delay*12];
in.read(SIZE_RSPACKET);
out.written(1);
/* Printing output after Deinterleaving to file: Need to Turn this into Binary */
FILE * F; // Create File
F = fopen("Deinterleaving.txt", "wb"); // Open Deinterleaving File
for(int i = 0; i < SIZE_RSPACKET; i++){ // For Every Packet (204 bytes)
// Print char for data coming and going out (Not Binary) to file
fprintf(F, "%s %u %s %u \n", " Data coming in: ", pin[i], " Data Going Out: ", pout[i]);
}
fflush(F);
fclose(F);
}
}
This gives me the output:这给了我输出:
Data coming in: 71 Data Going Out: 0
Data coming in: 99 Data Going Out: 0
Data coming in: 46 Data Going Out: 0
Data coming in: 84 Data Going Out: 0
Data coming in: 129 Data Going Out: 0
Data coming in: 134 Data Going Out: 0
Data coming in: 1 Data Going Out: 0
Data coming in: 101 Data Going Out: 0
Data coming in: 15 Data Going Out: 1
How can I convert this to it's binary counterpart for every 8 packets so that the output will look like the following?如何将其转换为每 8 个数据包的二进制对应物,以便输出如下所示?
What the output should look like (ie 1 block (8 Packets of 204 bytes each)):输出应该是什么样子(即 1 个块(8 个数据包,每个数据包 204 字节)):
71 99 46 84 129 134 1 101 -> 01000111 01100011 0101110 01010100 010000001 010000110 00000001 01100101
Use fwrite
for writing internal representation of data to a file:使用
fwrite
将数据的内部表示写入文件:
fwrite(&pin[0], 1, sizeof(pin), F);
Also, open the file in a binary mode to avoid translations, such as the value 0x0d
being replaced by 0x0d, 0x0a
.此外,以二进制模式打开文件以避免转换,例如将值
0x0d
替换为0x0d, 0x0a
。
The fprintf
is for transforming internal representation into human readable form. fprintf
用于将内部表示转换为人类可读的形式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.