Displaying Binary

Started by
10 comments, last by DigitalDelusion 18 years, 9 months ago
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.
Advertisement
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.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
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;    }}
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
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');}
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
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?
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.

Quote:Original post by 3dmodelerguy
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?


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




HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Have you considered using hex?
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
No I have not, are there any benifit of using Hex over Binary?
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"

This topic is closed to new replies.

Advertisement