Convert one hexadecimal character to its byte value in C #

This converts 1 hexadecimal character to its integer value, but it needs to build a (under) string.

Convert.ToInt32(serializedString.Substring(0,1), 16); 

Does .NET have a built-in way to convert a single hexadecimal character to its byte (or int, unimportant) value, which is not related to creating a new line?

+4
source share
7 answers
 int value = "0123456789ABCDEF".IndexOf(char.ToUpper(sourceString[index])); 

Or even faster (subtraction compared to array search), but without checking for bad input:

 int HexToInt(char hexChar) { hexChar = char.ToUpper(hexChar); // may not be necessary return (int)hexChar < (int)'A' ? ((int)hexChar - (int)'0') : 10 + ((int)hexChar - (int)'A'); } 
+16
source

Correct me if I am wrong, but you can just use

 Convert.ToByte(stringValue, 16); 

while stringValue represents a hexadecimal number? Isn't that the point of the base parameter?

Strings are immutable, I don’t think there is a way to get the byte value of the char substring at index 0 without creating a new line

+16
source

Of course, you can get the hexadecimal value without requiring another line to be created. I'm not sure that this will really benefit you, but since you asked, that’s what you asked.

  public int FromHex(ref string hexcode, int index) { char c = hexcode[index]; switch (c) { case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; case 'A': case 'a': return 0xa; case 'B': case 'b': return 0xb; case 'C': case 'c': return 0xc; case 'D': case 'd': return 0xd; case 'E': case 'e': return 0xe; case 'F': case 'f': return 0xf; case '0': default: return 0; } } } 
+2
source

If you know that the hexadecimal value is only a byte, then just convert to Int32 and then do

 var b = (byte)(Convert.ToInt32(serializedString, 16)); 
+1
source

Interestingly, something like this is faster? Or am I still thinking as a C programmer for C #?

 private static readonly sbyte[] _unhex_table = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, }; public static int HexCharToInt(char c) { return _unhex_table[c]; } 

called HexCharToInt(mystring[index]) and it is assumed that the string contains only extended ASCII.

0
source

I add another answer only because no one mentioned this. You can use the built-in Uri.FromHex method to convert a single character:

 var b = (byte) System.Uri.FromHex('a'); // b = 10 
0
source
 Encoding.UTF8.GetBytes( serializedString.ToCharArray(), 0, 1) 

It could be cheaper:

 Encoding.UTF8.GetBytes( new char[]{ serializedString[0] }, 0, 1) 

This will add an interesting char to char [], and not to the entire line.

-1
source

All Articles