Saturday, 10 December 2016

Convert C# bytes to human-readable strings

Problem:

How do I convert a C# byte into a string so that I can read all 8 digits?

Solution:

Imagine that you have a C# byte named myByte. To get a string containing all 8 binary digits, you can use the following code:

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

Notes:

Also remember that the Convert.ToString method is in the System namespace.

References:

https://msdn.microsoft.com/en-us/library/system.convert.tostring(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.string.padleft(v=vs.110).aspx http://stackoverflow.com/questions/4829366/byte-to-binary-string-c-sharp-display-all-8-digits

No comments:

Post a Comment