Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading in bits in a binary file 2

Status
Not open for further replies.

yaids

Programmer
Apr 1, 2005
11
AU
Hi, any help would be appreciated.
I am opening a binary file using an input filestream, and need to read in the first octet of bits (one byte?) to perform operations on.
Is there any way to just read in 8 bits? I tried doing this by reading in a 'char', but then realised that a char does not suffice as some combinations of binary numbers do not translate to a char.
Thanks in advance.
 
Of course, you may read one byte:
Code:
istream is;
char ch;
... // open a stream if it's closed.
is.get(ch); // for example, cin.get(ch);
You can't read a bit.
You may assign any value of 0..255 range to char. The only subtle point is a char type. This integral (i.e. integer) type may be signed or unsigned (as usually it's signed).

So I don't understand your statement:
realised that a char does not suffice as some combinations of binary numbers do not translate to a char. Explain, please...
 
Sorry, what I mean is, a char corresponds to an ASCII character, but there are only about 100 or so ASCII characters that exist. So if the bit encoding doesn't correspond or translate to an ASCII character, wouldn't that mean the bits are lost?
general example:
'1001 1001 = 'A'
But what if something like '1111 1111' does not equal anything in ASCII?
Correct me if I'm wrong. Thanks.
 
>But what if something like '1111 1111' does not equal anything in ASCII?

Then when display it will look funny, but there's of course nothing that stops you from reading the entire char.



/Per

www.perfnurt.se
 
So there definitely is no way to read in individual bits in a binary file?
I just need to be sure, because I read somewhere that C++ allows bitset operations.
 
Read a byte then select bits in it with bitwise operators (&, |, ^, ~, << and >>). What's a problem?
For example, select 3rd bit (from least to high, 0..7):
Code:
char ch;
cin.get(ch);
int bit3 = (ch >> 3)&1;
// or to check bit value:
if (((ch&8) == 1) // then bit on...
The type char in C/C++ is not true character type (as in some other languages). All three (slightly different!) char types (char, [/b]signed char[/b] and unsigned char) are semantically integers (at least 8 bits, but may be more;).
Apropos, bit fields in C was unfortunate and clumsy construct. In fact it's unportable and useless. But explicit bit selection is unportable too (think about this fact;)...
 
OK say I did read in the 8 bits as a char. How would I then go about converting that into an integer value? I have a function that converts from binary to integer by taking in a char* or string, but I don't know how I could do it with a char.
Any suggestions?
 
Let's cross our 't's...
If you read 8 bits in char, no need to convert it into an integer value because of it's integer value already (see my post above).

Another story - if you want to read a text representation of an integer value (for example, in binary form) then convert it into internal (machine) form (in int, for example) - it's quite another matter. You don't read bits in that case - you read a text representation of a binary value (for example, user types: 111, press enter, then your program must print Thank you for 7)...

What do you want in actual fact?
 
Hmm ok, I re-read your previous post and I think it answers my question. Yes, I wanted to convert 8 bits in char directly into an int (the first case that you mention). So then I could just typecast the char into an int?
 
Yes.
Code:
char c = 0xF5;
int i = static_cast<int>( c );
 
Strictly speaking, no need in an explicit cast op in the case because of there's intrinsic conversion (so called promotion) char to int (both are integral types and int >= char).
So you may write:
Code:
char ch;
int  ival;
...
ival = ch;
It's legal and safe op.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top