Hi everybody!

Now I'm dealing with binary file handling and I need to read 70 bits into a variable from a file.

As far as I know (correct me please, if I'm wrong), C does not have any variable type that could hold such large data.

(I think) I cannot use a string either as a string terminates with a null character and the binary I read from the file might contain a null character in the middle too.

I could have handled the data as two variables but it will give me a real hard time.

Does somebody have a better idea please?

Thanks in advance.

Recommended Answers

All 12 Replies

Why not read 128 bits and mask out the unwanted data.

Why not read 128 bits and mask out the unwanted data.

Sorry, this is just because I don't know. Is there any variable type in C that could hold 128 bits of data? Because as far as I found out the maximum length of a variable would be 80 bits.

Or if there is a way of doing it, please tell me, because that could easily solve my problem.

Thanks again.

Well if I had to do this I would create an array like(this assumes 64 bit longs)

unsigned long mya[2] = {0,0};/*128 bits initialized to zero*/

and read the data like so

fread(mya, sizeof(unsigned long), 2, fileStream);/*read 128 bits*/

you could create an 18-byte character array, assuming 8 bits to the byte. Files do not contain bits -- they contain bytes, and the number of bits to the byte is operating system dependent. On MS-Windows and *nix there's 8 bit to the byte. So 128 bits would be 128/8 = 18 bytes.

you could create an 18-byte character array, assuming 8 bits to the byte. Files do not contain bits -- they contain bytes, and the number of bits to the byte is operating system dependent. On MS-Windows and *nix there's 8 bit to the byte. So 128 bits would be 128/8 = 18 bytes.

That's odd, I always thought byte size was a feature of the hardware and not the operating system.

You are right. The number of bits in a byte is hardware dependent.

I always thought byte size was a feature of the hardware and not the operating system.

I was under the impression that 8bits = 1byte is a universally accepted measurement, just like 1 kilobyte = 1024 bytes. Meaning that whatever it is dependent on, everybody make their system such that 8bits = 1byte. Isn't it so? I'm amazed!!!

Files do not contain bits -- they contain bytes,

Right. But I want to read bits. You see, my task is to develop some kind of a crypto system which means I got to do a lot of bit substitutions and permutations in which case I certainly have to mask bits out.

This is exactly why I'm looking for a system that does not involve more than one variable to hold the data.

For an instance, if I want to exchange the positions of 7th bit and 60th bit, I'll have to check explicitly to see which variable the 7th bit belongs to as well as 60th bit belongs to in order to read them.

Well it seems that I cannot avoid using more than one variable here. So I'll try using few.

Thanks once again everybody.

>>I was under the impression that 8bits = 1byte is a universally accepted measurement,

Nope -- there are 4 and 7 bit byte hardware, but 8 bit is probably the most common.

>>But I want to read bits.
Impossible. You have to read bytes and then extract bits in memory.

>>This is exactly why I'm looking for a system that does not involve more than one variable to hold the data

You won't find it on MS-Windows, MS-DOS, or *nix systems because they have not invented those things yet. You might try IBM mainframe -- but you may have to pay a few million dollars to get it.

Right. But I want to read bits. You see, my task is to develop some kind of a crypto system which means I got to do a lot of bit substitutions and permutations in which case I certainly have to mask bits out.

You can't read bits outright, the hardware can only address bytes. If you need to interrogate bits, you must rely on masking operations or bit operators.

Quick question AD, which hardware does groups 4 bits into a byte ?

Read this wiki article. I have never actuall come across such a hardware myself, at least not that I know of.

You can't read bits outright, the hardware can only address bytes. If you need to interrogate bits, you must rely on masking operations or bit operators.

I thought I mentioned something about masking. Have I got a communication weakness? Doesn't what I write make sense?

You won't find it on MS-Windows, MS-DOS, or *nix systems because they have not invented those things yet. You might try IBM mainframe -- but you may have to pay a few million dollars to get it.

Well, this suggestion is a bit far-fetched for my economy :) Got to stick with more than one variable I guess. :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.