4

I need to convert a Byte[] to a string which contain a binary number sequence. I cannot use Encoding because it just encodes bytes into characters!

For example, having this array:

new byte[] { 254, 1 };

I want this output:

"1111111000000001"
10
  • 2
    Are you saying you want a string that's for example "0110010010100100"? Commented May 4, 2011 at 11:09
  • yep ! thats exactly i need i already have it on a byte[] Commented May 4, 2011 at 11:10
  • you need the bytes converted to string "as is"? Commented May 4, 2011 at 11:10
  • What encoding are you talking about? I mean, what is your idea about encoding that you don't want it? Commented May 4, 2011 at 11:11
  • 1
    @Sudantha give us the input and the value of the byte[] as an example. Commented May 4, 2011 at 11:14

5 Answers 5

13

You can convert any numeric integer primitive to its binary representation as a string with Convert.ToString. Doing this for each byte in your array and concatenating the results is very easy with LINQ:

var input = new byte[] { 254 }; // put as many bytes as you want in here
var result = string.Concat(input.Select(b => Convert.ToString(b, 2)));

Update:

The code above will not produce a result having exactly 8 characters per input byte, and this will make it impossible to know what the input was just by looking at the result. To get exactly 8 chars per byte, a small change is needed:

var result = string.Concat(input.Select(b => Convert.ToString(b, 2).PadLeft(8, '0')));
Sign up to request clarification or add additional context in comments.

2 Comments

Hi john i think this works for me.. i was able to count the length of the result... but when i try to take it to a textbox form docent load ... any idea ? :)
Thanks John ... i was able to take it to richTextBox .. THanks a lot for ur help and support
3
string StringIWant = BitConverter.ToString(byteData);

But i suggest work with Encoding..

string System.Text.Encoding.UTF8.GetString(byte[])

EDIT: For like 10101011010

From @Quintin Robinson's answer;

StringBuilder sb = new StringBuilder();
foreach (byte b in myByteArray)
    sb.Append(b.ToString("X2"));

string hexString = sb.ToString();

1 Comment

kind of comes a s FF-FF- ..... i think thats hexa .. do you know now to convert into binary 10101010
2

Perhaps you can use the BitArray class to accomplish what you are looking for? They have some sample code in the reference which should be pretty easy to convert to what you are looking for.

Comments

1

Will BitConverter.ToString work for you?

Comments

1
byte[] bList = { 0, 1, 2, 3, 4, 5 };
string s1 = BitConverter.ToString(bList);
string s2 = "";
foreach (byte b in bList)
{
     s2 += b.ToString();
}

in this case s1 ="01-02-03-04-05"
and s2= "012345"
I don't really get what are you trying to achieve.

1 Comment

Personally I've understood what he wants to achieve. What I really don't understand is what are you trying to achieve in this example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.