将二进制字符串作为二进制写入文件
我想将一个仅包含零和一的字符串写入输出文件,其中输出文件实际上仅包含按字符串给出的顺序的零和一。
目前我正在尝试将字符写入大小为1位的
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *out_file;
out_file = fopen("out", "wb");
char s[] = "01010101010101010101"; /*20 chars*/
int i;
for (i = 0; i < 20; i++) {
fwrite(&s[i], 1, 1, out_file);
}
fclose(out_file);
return EXIT_SUCCESS;
}
十六进制编辑器中的输出文件
30 31 30 31 30 31 30 31 30 31 30 31 30 31 30 31 30 31 30 31
因此输出文件包含ascii值零和一,而不是实际的零和一。
谢谢
I want to write a string consisting of only zeroes and ones to a output file where the output file literally only contains the zeroes and ones in the order given by the string.
At the moment I am trying to write the char to a size 1 bit
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *out_file;
out_file = fopen("out", "wb");
char s[] = "01010101010101010101"; /*20 chars*/
int i;
for (i = 0; i < 20; i++) {
fwrite(&s[i], 1, 1, out_file);
}
fclose(out_file);
return EXIT_SUCCESS;
}
The output file in a hex editor
30 31 30 31 30 31 30 31 30 31 30 31 30 31 30 31 30 31 30 31
Thus the output file contains the ascii values for zero and one, and not actual zeros and ones.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在字符串文字中使用转义序列来表示字节
0x00
和0x01
,而不是字符0
和1
:Use escape sequences within the string literal to denote the bytes
0x00
and0x01
, not the characters0
and1
:使用字节类型而不是字符并删除那些字符串,当然它显示0x30代表0,你要求它写'0'而不是0....
您可以使用 b byte b1 = 10001110b 指定二进制;
当然,你不能用它来创建一个数组,如果你想要更长的数字,你必须一一编写它们,或者编写一个方法\函数来解析二进制字符串,如果开源中不存在的话已经。
use byte type and not char and remove those string, of course it shows 0x30 for 0, you asked it to write '0' not 0....
you can specify binary by using b byte b1 = 10001110b;
of course you can't make an array out of it, if you want longer numbers u'll have to write them one by one or write a method\function to parse binary strings for ya, if one doesn't exist in open source already.
如果您尝试将字符串“011000100110100101110100”表示为三个字节值 98、105、116,您首先需要将它们从字符数组(或 C 中称为“字符串”)转换为数组数值。
像这样的事情可能是一个好的开始:
该例程逐步遍历输入字符串并将其转换为其表示的二进制数。它可能需要进行一些整理,因为它不一定是可读代码的典范,并且可能应该返回分配的字节数并采用范围限制器而不是依赖 strlen。
If you're trying to express the string "011000100110100101110100" as the three byte values 98, 105, 116 you will first need to convert them from an array of chars (or "string" as it's called in C) to an array numerical values.
Something like this may be a good start:
This routine steps through the input string and converts it to the binary number(s) it represents. It could probably do with some tidying-up, since it's not necessarily a paragon of readable code and should probably return the number of bytes allocated and take a range limiter instead of relying on strlen.
好的,这是您的解决方案(虽然还没有经过非常彻底的测试):
您可能还想考虑阅读 这里的线程可以让您的工作更轻松。
代码没有优化。您可以将其用于任何目的,甚至商业用途(如果您能够出售它)。超过 8 位的字符串将变得不可读,因此您可能只想手动调用 CharArrayToByte (确保 char 数组长度确实是 8 个字符,否则会遇到问题)。如果您想将其转换回来,只需颠倒该过程即可。
目前尚不清楚您使用的是 C 还是 C++(使用 C 编码),因此您可能必须对其进行修改以满足您的需求。
Okay, here's your solution (it's not very thoroughly tested though):
You may want to consider also reading this thread here to make your job easier.
The code isn't optimised. You can use it for whatever purpose, even commercial (if you're able to sell it). Strings will get unreadable beyond 8 bits, so you may just wish to call CharArrayToByte manually (ensure the char array length is indeed 8 characters or you will have issues). If you want to convert it back, it's a matter of reversing the process.
It wasn't clear whether you were using C or C++ (with C coding), so you'll likely have to modify it to suit your needs.