Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a binary file and putting the data into an array

Status
Not open for further replies.

drrep

Programmer
Mar 13, 2002
47
US
Hi everyone,

Ok, I have a binary file which has 400 lines and contains 50 bytes per line.

Problem is of course, the file is in binary and when I open it in a regular text editor, it's all garbage that's displayed. Apparently, that's because, from what I understand, the data is compressed.

How can I uncompress it such that each byte from the 50 byte line displays 8 bits each, thus 0 and 1's so I can put this into an array and continue on?

Is it with the fopen command where I must add "rb", b being for binary that it reads correctly or is there an actual algorithm to transform each byte into bits of 0 or 1?

Any help/ideas would be appreciated...
 
If you are wanting to view the '1's and '0's then you'll have to manually manipulate things at the bit level. I'm not really sure what you're trying to achieve with this?? If you have a line of bytes (50 long) sucked in from a file then each of those bytes will contain 8 bits. It sounds like you're wanting to view the bits rather than the bytes???????

Each byte can hold a value range of 256. So, if you examine the value of a particular byte from the line, you can work out which bits are set and which are not in that single byte. For example, if the value of the byte is 5 then you know that bits 1 and 3 are set and the rest are not.

A classic use of this is to test whether a number is odd. Because if a number is odd then bit number 1 is always set:

int i = 7;

if (i & 1)
{
// we must be odd
}

 
Is there a function that takes the byte and transforms it into a value of bits?

 
hi, I don't remeber if there is,
but you can enjoy to build.

To do these things you have to use 2
special C operator (in this C likes assembler):

1) AND Bitwyse operator & ( not && logical AND )
2) SHIFT Bit Operators : >> shift right, << s.left.

Each byte is done by 8 bits: you have to test
if for bit = 0 .. 7 each one is 1 or 0 and
print 1 or 0 as a char.

char c='A' ;

If we printf( &quot;%c&quot;, c ) --> we see A
if we printf( &quot;%d&quot;, c ) --> we see 65 ( in binary '1000001')
64+1


char mask= 128; //we build a mask 10000000 .
char shiftedmask ;
for( int i=0 ; i<8 ; i++ )
{
shiftedmask = mask >> i ;
if( shiftedmask & c )
printf( &quot;1&quot; ) ;
else
printf( &quot;0&quot; ) ;
}

OR

char BitRap[9];
for( int i=0 ; i<8 ; i++ )
{
shiftedmask = mask >> i ;
BitRap = '0' + (shiftedmask & c) ? 1 : 0 ;
}
BitRap[8]= '\0' ;
printf(&quot;%s\n&quot;, BitRap ) ;

 
Another possible way is to use the 'itoa' function:

char szByte[9];
int i;
BYTE bArr[400];

// Read Bytes into array here

for (i=0; i < 400; i++)
{
cout << &quot;Byte #&quot; << itoa(i, szByte, 10) << &quot;: &quot;;
cout << itoa(bArr, szByte, 2) << endl;
}

This should print out the bytes in binary. Hope this helps!

--Rob



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top