Displaying Binary
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.
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.
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; }}
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');}
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement