Displaying Binary

This topic is 4767 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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 ) << 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 );    for ( int i = 0 ; i < 8 ; ++i ) {        bool bit = bits;        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"

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
19
5. 5

• 14
• 26
• 11
• 11
• 9
• Forum Statistics

• Total Topics
631770
• Total Posts
3002251
×