467,892 Members | 1,851 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,892 developers. It's quick & easy.

Writing Binary Digits to a file

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 and
then 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 I
could store as bits would only take up around 7 KB.

Could you please tell me if this could be done.

Thanks a ton.
Speed.

Oct 5 '06 #1
  • viewed: 2727
Share:
5 Replies
Speed wrote:
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
Huh?
and
then fwrite to store it as follows.

fwrite( BinArr, sizeof(unsigned char), 57000, BinFile);
Why are you writing 57000 bytes? Shouldn't you be writing 57000/CHAR_BIT?
But it is a waste of space as this takes up around 57 KB which if I
could store as bits would only take up around 7 KB.
Yes. Don't you already map each bit of your 'BinArr' to the binary digit
in your number? If not, you should.
Could you please tell me if this could be done.
Not sure what you mean. When you say you have "a 57000 bit long binary
number", how do you represent it?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 5 '06 #2
"Speed" <lo**********@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
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 and
then 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 I
could store as bits would only take up around 7 KB.

Could you please tell me if this could be done.

Thanks a ton.
Speed.
Sure, just store the bytes as you're using them. You say it's a "binary
number". How is it stored in variables? BCD (Binary Coded Decimal)? Some
other representation? How much memory does it take to store them in the
program?
You say you have a "57000 *bit* long binary number*. Is it 57,000 bits or
digits? If it's 57000 bits, then just store them as binary.

unsigned BinArr char[57000 / sizeof( unsigned char )]; // this would be
57,000 *bits*
// This array is loaded/used somewhere, or how are you using these 57,000
bits?

fwrite( BinArr, sizeof( unsigned char), 57000 / sizeof( unsigned char),
BinFile );

57000 / 8 (sizeof unsigned char on my system) is 7125, so these 57000 bits
are stored in 7125 bytes.
Oct 7 '06 #3
Jim Langston wrote:
"Speed" <lo**********@gmail.comwrote in message
>I have a 57000 bit long binary number and I want to save it to a file.
[...]
You say you have a "57000 *bit* long binary number*. Is it 57,000 bits or
digits? If it's 57000 bits, then just store them as binary.

unsigned BinArr char[57000 / sizeof( unsigned char )]; // this would be
57,000 *bits*
// This array is loaded/used somewhere, or how are you using these 57,000
bits?

fwrite( BinArr, sizeof( unsigned char), 57000 / sizeof( unsigned char),
BinFile );

57000 / 8 (sizeof unsigned char on my system) is 7125, so these 57000 bits
are 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
Oct 7 '06 #4
Thomas J. Gritzan posted:
You want CHAR_BITS.

Singular.

#include <climits>

CHAR_BIT

--

Frederick Gotham
Oct 7 '06 #5
"Thomas J. Gritzan" <Ph*************@gmx.dewrote in message
news:eg**********@newsreader2.netcologne.de...
Jim Langston wrote:
>"Speed" <lo**********@gmail.comwrote in message
>>I have a 57000 bit long binary number and I want to save it to a file.
[...]
>You say you have a "57000 *bit* long binary number*. Is it 57,000 bits
or
digits? If it's 57000 bits, then just store them as binary.

unsigned BinArr char[57000 / sizeof( unsigned char )]; // this would be
57,000 *bits*
// This array is loaded/used somewhere, or how are you using these 57,000
bits?

fwrite( BinArr, sizeof( unsigned char), 57000 / sizeof( unsigned char),
BinFile );

57000 / 8 (sizeof unsigned char on my system) is 7125, so these 57000
bits
are 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.
Ack, my bad!
Oct 7 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Rim | last post: by
12 posts views Thread by neutrino | last post: by
6 posts views Thread by Sebastian Kemi | last post: by
6 posts views Thread by Nick R | last post: by
4 posts views Thread by jesuraj | last post: by
18 posts views Thread by Alex Buell | last post: by
21 posts views Thread by nicolasg | last post: by
1 post views Thread by Alex Buell | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.