Reputation: 9235
I would like to convert an Int32
in the range 0-15
into a the corresponding char
in hexadecimal. One really dummy solution consists in writing
var hex = new[] {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
var myCharInHex = hex[myValue];
Yet, this solution looks plain wrong, any better suggestion?
Upvotes: 0
Views: 300
Reputation: 5793
This simple code must work:
string hexValue = myValue.ToString("X");
Upvotes: 1
Reputation: 1499770
That works for your exact specification, but I'd personally do it as:
private static readonly char[] HexDigits = "0123456789abcdef".ToCharArray();
Upvotes: 4