Converting a Byte to a Binary String in C#

Converting a byte to a binary string in c#

Just change your code to:

string yourByteString = Convert.ToString(byteArray[20], 2).PadLeft(8, '0');
// produces "00111111"

Convert a byte[] to its string representation in binary

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')));

convert byte to binary

If you want a bit-string:

byte b = 100;

//Will be "1100100"
var bitstring = Convert.ToString(b, 2);

so in your example, just add , 2

The second argument is the base you want to use.

  • 2 = binary (0 - 1)
  • 8 = octal (0 - 7)
  • 16 = hex (0 - F)

and secondary, I have a little improvement on your code :) this will do:

byte[] myArray = System.IO.File.ReadAllBytes(@"myFlvFile.flv");

for (r = 0; r < 50; r++)
{
txtBit.Text = Environment.NewLine + Convert.ToString(myArray[r], 2);
//Or if you want to append instead of replace? (I think you do, but that is not what your code do)
txtBit.Text += Environment.NewLine + Convert.ToString(myArray[r], 2);
}

there is still stuff to improve on - you may want to look at StringBuilder or similar :-) (it is quite inefficient to concatenate the text the way you do it.)

How can I convert a C# byte array into a binary string to be pasted in a SQL script?

Binary constants in SQL Server 'have the prefix 0x and are a string of hexadecimal numbers'.

You can convert each byte to hexadecimal using the "x2" format string and combine the result with String.Join():

var bytes = new byte[] {1, 2, 3, 4, 5, 6};
var binaryString = "0x" + string.Join("", bytes.Select(b => b.ToString("x2")));

Byte to Binary String C# - Display all 8 digits

Convert.ToString(MyVeryOwnByte, 2).PadLeft(8, '0');

This will fill the empty space to the left with '0' for a total of 8 characters in the string

Best method to convert byte-array's elements to binary

Instead of working on the string representation, work on bits directly.

If your array value is 5A, testing bit 4 can be done so:

if ((arr[byteIndex] & (1 << 4)) != 0) { }

Explaination:

arr[byteIndex] = 01011010
1<<4 = 00010000
-------- bitwise &
result = 00010000

How to convert byte array to string

Depending on the encoding you wish to use:

var str = System.Text.Encoding.Default.GetString(result);

C# converting byte array to string

If you pad with zeros you'll get the answer

public static void Main()
{
StringBuilder str = new StringBuilder();
byte[] x = { 0xB1, 0x53, 0x63 };
for (int i = 0; i < 3; i++)
{
str.Append(Convert.ToString(x[i], 2).PadLeft(8, '0'));
}
Console.WriteLine(str);
Console.ReadLine();
}

Fiddle



Related Topics



Leave a reply



Submit