For instance mask with 0xF000 >> 12 gives you an index into the hex string that represents the highest order 4 bits; 0xF00 >> 8 gives next 4, and so on
Below is a little code to make it an extension method, so you can use it like this:
byte b = 42;
b.ToHexString();
namespace TestProgram
{
public static class Extensions
{
const string hex = "0123456789ABCDEF";
public static string ToHexString(this byte b)
{
int lowNibble = b & 0x0F;
int highNibble = (b & 0xF0) >> 4;
string s = new string(new char[] { hex[highNibble], hex[lowNibble] });
return s;
}
public static string ToHexString(this int i)
{
int nibble0 = i & 0x000F;
int nibble1 = (i & 0x00F0) >> 4;
int nibble2 = (i & 0x0F00) >> 8;
int nibble3 = (i & 0xF000) >> 12;
string s = new string(new char[] { hex[nibble3], hex[nibble2],
hex[nibble1], hex[nibble0] });
return s;
}
}
}
namespace System.Runtime.CompilerServices
{
public class ExtensionAttribute : Attribute { }
}