This is the one I used:
private static string ConvertToHex(byte[] bytes) { var builder = new StringBuilder(); var hexCharacters = new[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; for (var i = 0; i < bytes.Length; i++) { int firstValue = (bytes[i] >> 4) & 0x0F; int secondValue = bytes[i] & 0x0F; char firstCharacter = hexCharacters[firstValue]; char secondCharacter = hexCharacters[secondValue]; builder.Append("0x"); builder.Append(firstCharacter); builder.Append(secondCharacter); builder.Append(' '); } return builder.ToString().Trim(' '); }
And then used as:
string test = "1234"; ConvertToHex(Encoding.UTF8.GetBytes(test));
Davin tryon
source share