views:

156

answers:

2

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?

+4  A: 

That works for your exact specification, but I'd personally do it as:

private static readonly char[] HexDigits = "0123456789abcdef".ToCharArray();
Jon Skeet
+1  A: 

This simple code must work:

string hexValue = myValue.ToString("X");
tekBlues
but it returns a string, not a char
tanascius