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!

Want to store/print binary values with 8 characters (bits)

Status
Not open for further replies.

drrep

Programmer
Mar 13, 2002
47
US
Hello everyone,

I am using the following code to transfer bytes to binary:

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;
}


Problem with the above code, eventhough it rocks (thanks to some help from this site), it gives the following as output:

11000110
1111
0
101010

Thing is, I want to find a way to pad the blanks with 0's to display a FULL eight bits....BUT, I cannot put 4 0's after say 1111 (1111 0000) because that's not the value it's representing, rather it has to be 0000 1111. I need to find a way to do that without affecting the other values?

Do I need to use masking? If so, how do I use that?

Thanks in advance!
 
itoa(bArr, szByte, 2);
cout << (&quot;00000000&quot;+strlen(szByte)) << szByte << endl;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top