65

In c# I am converting a byte to binary, the actual answer is 00111111 but the result being given is 111111. Now I really need to display even the 2 0s in front. Can anyone tell me how to do this?

I am using:

Convert.ToString(byteArray[20],2)

and the byte value is 63

1

4 Answers 4

79

Just change your code to:

string yourByteString = Convert.ToString(byteArray[20], 2).PadLeft(8, '0');
// produces "00111111"
Sign up to request clarification or add additional context in comments.

7 Comments

this method will always make the string with 8 digits right? so if i will get a different value for instance 10001111 this will not add new 0s in front
@IanCian correct, no matter what there will always be 8 digits so if you supply them all, the PadLeft will do nothing but if you don't, it iwll fill in the left over space to the left with 0s.
You're right -- my suggestion wasn't right. I didn't test it.
@Iancian Did this end up solving your issue?
@Kelsey It does this because it knows that byte is always 8 in length when converting the byte to string? Is that why it always works?
|
32

If I understand correctly, you have 20 values that you want to convert, so it's just a simple change of what you wrote.

To change single byte to 8 char string: Convert.ToString( x, 2 ).PadLeft( 8, '0' )

To change full array:

byte[] a = new byte[] { 1, 10, 100, 255, 200, 20, 2 };
string[] b = a.Select( x => Convert.ToString( x, 2 ).PadLeft( 8, '0' ) ).ToArray();
// Returns array:
// 00000010
// 00010100
// 11001000
// 11111111
// 01100100
// 00001010
// 00000001

To change your byte array to single string, with bytes separated with space:

byte[] a = new byte[] { 1, 10, 100, 255, 200, 20, 2 };
string s = string.Join( " ",
    a.Select( x => Convert.ToString( x, 2 ).PadLeft( 8, '0' ) ) );
// Returns: 00000001 00001010 01100100 11111111 11001000 00010100 00000010

if ordering of bytes is incorrect use IEnumerable.Reverse():

byte[] a = new byte[] { 1, 10, 100, 255, 200, 20, 2 };
string s = string.Join( " ",
    a.Reverse().Select( x => Convert.ToString( x, 2 ).PadLeft( 8, '0' ) ) );
// Returns: 00000010 00010100 11001000 11111111 01100100 00001010 00000001

Comments

3
public static string ByteArrayToString(byte[] ba)
{
    StringBuilder hex = new StringBuilder(ba.Length * 2);
    foreach (byte b in ba)
        hex.AppendFormat("{0:x2}", b);
    return hex.ToString();
}

1 Comment

This code gives output like this: 55 4d 04 a8 4f 2a cd 01 40 00 81 57 4d 32 36 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 0f 00 00 00 00 00 00 02 0f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 06 a5
2

Try this one:

public static String convert(byte b)
{
    StringBuilder str = new StringBuilder(8);
            int[] bl  = new int[8];

    for (int i = 0; i < bl.Length; i++)
    {               
        bl[bl.Length - 1 - i] = ((b & (1 << i)) != 0) ? 1 : 0;
    }

    foreach ( int num in bl) str.Append(num);

    return str.ToString();
}

Comments

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.