473,386 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
5 3045
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rim | last post by:
Hi, >>> print '%x' % 54 36 >>> print '%b' % 54 Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: unsupported format character 'b' (0x62) at index 1 >>>
12
by: neutrino | last post by:
Greetings to the Python gurus, I have a binary file and wish to see the "raw" content of it. So I open it in binary mode, and read one byte at a time to a variable, which will be of the string...
6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
6
by: Nick R | last post by:
I need to know how to input bits from a *.wav file. I have tried this: #include <fstream> .... ifstream fin; fin.open("/root/foofile.wav", ios::in|ios::binary); and then inputting...
4
by: jesuraj | last post by:
Hi, how can i read input from a data file which contains binary or hex values. I have to use the exact binary data for further processing.only limited number of bits are taken form the file(64...
68
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
18
by: Alex Buell | last post by:
Here's something I put together just now; whilst I don't know enough to make this act the same way std::hex and std::dec does, this works quite well enough for me, but I think I could improve on...
21
by: nicolasg | last post by:
does anyone know a module or something to convert numbers like integer to binary format ? for example I want to convert number 7 to 0111 so I can make some bitwise operations... Thanks
1
by: Alex Buell | last post by:
Right... here it is - flames and comments all welcome: #ifndef __BINARY__ #define __BINARY__ #include <iostream> #include <string> #include <limits> #include <exception>
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.