473,327 Members | 2,055 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,327 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 3041
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.