Если нет ничего очевидного, возможно, просто цикл:
public static string ToHexString(byte[] raw)
{ // could also be an extension method
StringBuilder sb = new StringBuilder("0x", 2 + (raw.Length * 2));
for (int i = 0; i < raw.Length; i++)
{
sb.Append(raw[i].ToString("X2"));
}
return sb.ToString();
}
Если это свойство класса, было бы тривиально создать TypeConverter
, который делает это (для целей отображения), и пометить свойство как [TypeConverter(typeof(HexConverter))]
:
class HexConverter : TypeConverter // untested
{
public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture,
object value, Type destinationType)
{
if (destinationType == typeof(string))
{
return ToHexString((byte[])value);
}
return base.ConvertTo(context, culture, value, destinationType);
}
}