本文介绍了将二进制数字写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个57000位长的二进制数,我想将它保存到文件中。 目前我使用unsigned char数组来存储数组和 然后fwrite存储它如下。 fwrite(BinArr,sizeof(unsigned char), 57000,BinFile); 但这是浪费空间,因为这需要大约57 KB,如果我可以存储,因为比特只会占用大约7 KB。 你能否告诉我是否可以这样做。 非常感谢。 速度。 解决方案 嗯? 为什么要写57000字节?难道你不是在写57000 / CHAR_BIT吗? 是的。你不是已经将你的''BinArr''的每一位映射到你的号码中的二进制数字 了吗?如果没有,你应该。 不确定你的意思。如果你说你有一个57000位长的二进制 数字,你怎么代表它? V - 请在通过电子邮件回复时删除资金''A' 我没有回复最热门的回复,请不要问 当然,只需存储使用它们的字节。你说它是'二进制 数字'。它是如何存储在变量中的? BCD(二进制编码十进制)?一些 其他代表?将它们存储在 程序中需要多少内存? 你说你有一个57000 * bit *长二进制数*。它是57,000位还是 位数?如果它是57000位,那么只需将它们存储为二进制。 unsigned BinArr char [57000 / sizeof(unsigned char)]; //这将是 57,000 *位* //这个数组是在某处加载/使用的,或者你是如何使用这些57,000 比特? fwrite(BinArr,sizeof(unsigned char),57000 / sizeof(unsigned char), BinFile); 57000/8(我系统中unsigned char的sizeof)是7125,所以这些57000位 存储在7125字节中。 Jim Langston写道: [...] sizeof(char)和sizeof(unsigned char)是 - 按照定义 - 1. 所以57000 / sizeof(unsigned char)是57000.你想要CHAR_BITS。 - Thomas http://www.netmeister.org/news/learn2quote.html Hi,I have a 57000 bit long binary number and I want to save it to a file.At the moment I am using an unsigned char array to store the array andthen fwrite to store it as follows.fwrite( BinArr, sizeof(unsigned char), 57000, BinFile);But it is a waste of space as this takes up around 57 KB which if Icould store as bits would only take up around 7 KB.Could you please tell me if this could be done.Thanks a ton.Speed. 解决方案Huh?Why are you writing 57000 bytes? Shouldn''t you be writing 57000/CHAR_BIT?Yes. Don''t you already map each bit of your ''BinArr'' to the binary digitin your number? If not, you should.Not sure what you mean. When you say you have "a 57000 bit long binarynumber", how do you represent it?V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t askSure, just store the bytes as you''re using them. You say it''s a "binarynumber". How is it stored in variables? BCD (Binary Coded Decimal)? Someother representation? How much memory does it take to store them in theprogram?You say you have a "57000 *bit* long binary number*. Is it 57,000 bits ordigits? If it''s 57000 bits, then just store them as binary.unsigned BinArr char[57000 / sizeof( unsigned char )]; // this would be57,000 *bits*// This array is loaded/used somewhere, or how are you using these 57,000bits?fwrite( BinArr, sizeof( unsigned char), 57000 / sizeof( unsigned char),BinFile );57000 / 8 (sizeof unsigned char on my system) is 7125, so these 57000 bitsare stored in 7125 bytes.[...]sizeof(char) and sizeof(unsigned char) is - by definition - 1.So 57000 / sizeof(unsigned char) is 57000. You want CHAR_BITS.--Thomas http://www.netmeister.org/news/learn2quote.html 这篇关于将二进制数字写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-01 10:07