# Displaying Binary

## Recommended Posts

Ok, I have a problem, i want to be able to open any type of file in binary and display ones and zeros(00100101110101) but what i get when using char buffer[100]; std::ifstream test ("test.bin", std::ios::in | std::ios::binary); test.read(buffer, 100); std::cout << buffer << std::endl; is: ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠░ ↕ how do i show it as 1's and 0's. do i have to do some bit shifting and if yes how do i do that. thanks.

##### Share on other sites
void ShowBits(char ch){	for(int i = 8; i--;)		putchar( '0' + ((ch >> i) & 1));}

and then:
char buffer[100];std::ifstream test ("test.bin", std::ios::in | std::ios::binary);test.read(buffer, 100);std::for_each( buffer, buffer + sizeof(buffer), ShowBits);

so yes you'll need to do some shifting and masking put not that complicated see ShowBits.

##### Share on other sites
Just rewriting into slightly more readable code, so he can get the concept as well. Putting a decrement in the test is just downright ugly.
void ShowBits(char ch) {    // For each bit in a byte (8 to 1)    for(int i = 8; i > 0; i--) {        // If the high bit is set, output a '1', else a '0'.        putchar( (ch & 128) ? '1' : '0' );        // Shift all bits up by one.        ch = ch << 1;    }}

##### Share on other sites
or why not:
void ShowBits(char ch){//start with top bit and then move the mask downwards.	for(int bit = 0x80; bit; bit >>= 1)		putchar( ch & bit ? '1' : '0');}

##### Share on other sites
ok, cool i got that working and i have been trying to firgure out a way of storing the file a binary. the ultimite goal of this is i wait to be able to compare 2 files at the binary level and then change only the binary numbers that a different so instead of coping the whole file over the old, i only changes a small part of the existing file. how would i go about this?

##### Share on other sites
The two best analagous versions I can come up with:

//Version 1:char buffer[ 100 ];std::ifstream test ( "test.bin" , std::ios::in | std::ios::binary );for ( int i = 0 ; i < 100 ; ++i )    std::cout << std::setw( 8 ) << std::fill( '0' ) << std::bitset< 8 >( buffer[i] ) << std::endl;}//setw and fill are used so that all 8 bits of the bitset are shown. Otherwise://00101100 would print as: 101100 which makes no sense if we want a bit stream.//Version 2:char buffer[ 100 ];std::ifstream test ( "test.bin" , std::ios::in | std::ios::binary );for ( int i = 0 ; i < 100 ; ++i ) {    std::bitset< 8 > bits( buffer[i] );    for ( int i = 0 ; i < 8 ; ++i ) {        bool bit = bits[i];        std::cout << (bit ? '1' : '0') << std::endl;    }}//printed out bit by bit, should make sense.

##### Share on other sites
Quote:
 Original post by 3dmodelerguyok, cool i got that working and i have been trying to firgure out a way of storing the file a binary. the ultimite goal of this is i wait to be able to compare 2 files at the binary level and then change only the binary numbers that a different so instead of coping the whole file over the old, i only changes a small part of the existing file. how would i go about this?

The file is already binary the conversion is just needed for display.
For the second part you could simply delta encode the difference and then simply store the offsets for diffrent blocks. My intution says that xor would work nicely for this. If you had (in binary)
   00001000^  00010100-----------   00011100

you could store that as
offset: 3
length: 3
delta: 111

and then to convert the old file to the new you would simply do:

   00001000^     111  -----------   00010100

tada! easy as a sunday morning

##### Share on other sites
Have you considered using hex?

##### Share on other sites
No I have not, are there any benifit of using Hex over Binary?

##### Share on other sites
Clicky

You might find that item particularly useful...

For speed, the smallest chunks of data you should probably deal with are bytes.

Also, the general rule in computers is "It's all binary anyway unless you want to show it to the user"

##### Share on other sites
I just wrote this. Would this work? It seems to:
#include <fstream>#include <iostream>using namespace std;struct Byte{    unsigned Bit1 : 1;    unsigned Bit2 : 1;    unsigned Bit3 : 1;    unsigned Bit4 : 1;    unsigned Bit5 : 1;    unsigned Bit6 : 1;    unsigned Bit7 : 1;    unsigned Bit8 : 1;};int main(int argc, char **argv){    Byte TestByte;        fstream File("TestFile.txt", ios::in);        File.read((char*)&TestByte, sizeof(char));  //I use sizeof char so the struct padding won't matter        cout << "First Byte: " << TestByte.Bit1 << TestByte.Bit2 << TestByte.Bit3                           << TestByte.Bit4 << TestByte.Bit5 << TestByte.Bit6                           << TestByte.Bit7 << TestByte.Bit8 << endl << endl;                               File.read((char*)&TestByte, sizeof(char));        cout << "Second Byte: "<< TestByte.Bit1 << TestByte.Bit2 << TestByte.Bit3                           << TestByte.Bit4 << TestByte.Bit5 << TestByte.Bit6                           << TestByte.Bit7 << TestByte.Bit8 << endl << endl;        File.close();        system("pause");    return 0;}

Here's the text file (Don't laugh at my stupid text file):
Don't eat the purple ones or your hair will fall out. Don't let your hair fall outor you will be bald. Don't become bald or people will laugh at you. Don't letpeople laugh at you or you'll lose your self-esteem. Don't lose your self-esteemor you'll want to commit suicide. Don't commit suicide or you'll die.

And here is the output:
First byte: 00100010Second Byte: 11110110Press any key to continue . . .

I'm not a good programmer, so I don't know if this actually works. I hope there is something useful here.

Edit: I just realized that you wanted a binary file, but I guess it doesn't really matter. It ought to still work.

[Edited by - Drakkcon on July 2, 2005 12:33:10 AM]

##### Share on other sites
the layout of bitfields are implementation defined so you can't really use them in the way outlined in the post above since the compiler is free to assign the bits any way it sees fit.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627657
• Total Posts
2978472

• 10
• 12
• 22
• 13
• 33