Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想将Int32范围内的一个转换0-15为对应char的十六进制。一个真正愚蠢的解决方案是写作
Int32
0-15
char
var hex = new[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; var myCharInHex = hex[myValue];
然而,这个解决方案看起来完全错误,有更好的建议吗?
这适用于您的确切规格,但我个人会这样做:
private static readonly char[] HexDigits = "0123456789abcdef".ToCharArray();
这个简单的代码必须有效:
string hexValue = myValue.ToString("X");