Showing a byte in binary / in octal - C#

To display an 8-bit byte in binary, code the following:
 byte xyz = 27;
 string xyz_binary = Convert.ToString(xyz, 2).PadLeft(8, '0');
For octal (with always leading 0), do:
 byte xyz = 27;
 string xyz_octal = Convert.ToString(xyz, 8);
 if (xyz_octal.Length > 1)
    xyz_octal = "0" + xyz_octal;
Print | posted on Wednesday, January 16, 2013 3:07 PM

Feedback

No comments posted yet.
Title  
Name
Email (never displayed)
Url
Comments   
Please add 1 and 5 and type the answer here: