Sign in to follow this  

Displaying Binary

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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




Share this post


Link to post
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 this post


Link to post
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 out
or you will be bald. Don't become bald or people will laugh at you. Don't let
people laugh at you or you'll lose your self-esteem. Don't lose your self-esteem
or you'll want to commit suicide. Don't commit suicide or you'll die.








And here is the output:

First byte: 00100010

Second Byte: 11110110

Press 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 this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this