public string ToHex() { if (BitLength == 0) { return(""); } var bytes = new byte[] { }; // Make a padded BitString if the length isn't % 8 if (BitLength % 8 != 0) { var padding = BITSINBYTE - BitLength % BITSINBYTE; var paddedBS = new BitString(BitLength + padding); for (var i = 0; i < BitLength; i++) { paddedBS.Set(i + padding, Bits[i]); } bytes = paddedBS.ToBytes(); } else { bytes = ToBytes(); } StringBuilder hex = new StringBuilder(bytes.Length * 2); for (int index = 0; index < bytes.Length; index++) { hex.AppendFormat("{0:x2}", bytes[index]); } return(hex.ToString().ToUpper()); }