Read binary

  • Thread starter Thread starter Arjen
  • Start date Start date
A

Arjen

Hi,

I can read in a file with the binaryreader. But how can I get all the bits
("1" and "0") inside my textbox?
(I now get the number 68)

Thanks!
 
One fine day in the middle of the night Arjen wrote in
Hi,

I can read in a file with the binaryreader. But how can I get all the
bits ("1" and "0") inside my textbox?
(I now get the number 68)


You're going to have to parse the number.


First you need to know the size of your value ( 68 appears to be a byte,
so the size would be 8... 8 bits for a byte )

Then you need a mask that you'll walk over your number. Start your mask
at a value of 0x80 and walk down from there. Then create a string to hold
your final binary number. Then walk the mask down the value:


string ConvertHexToBinary( byte myByte /* = 0x44 */ )
{
byte mask = 0x80;
string myBinaryOutput = "";

for( int i = 0; i < 8; i++ )
{
String bit = "";
if( ( myByte & mask ) != 0 )
bit = "1";
else
bit = "0";

myBinaryOutput += bit;
mask >>= 1;
}

return myBinaryOutput;
}

Hope that helps you.
 
Arjen said:
I can read in a file with the binaryreader. But how can I get all the bits
("1" and "0") inside my textbox?
(I now get the number 68)

So is your problem really just trying to convert an integer into a
binary string? If so, use Convert.ToString(val, 2).
 
I did not know this overload function.

Thanks!


Jon Skeet said:
So is your problem really just trying to convert an integer into a
binary string? If so, use Convert.ToString(val, 2).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top