6

This will convert 1 hex character to its integer value, but needs to construct a (sub) string.

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

Does .NET have a built-in way to convert a single hex character to its byte (or int, doesn't matter) value that doesn't involve creating a new string?

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
nos
  • 223,662
  • 58
  • 417
  • 506
  • None that I know of (short of writing it yourself), but what you're asking for is a micro-optimization that is very unlikely to give you any benefits, anyway. – Pavel Minaev Jul 31 '09 at 21:29
  • You're likely right. It just seems like a big waste creating > 120000 of these small objects per second here – nos Jul 31 '09 at 21:36

6 Answers6

20
int value = "0123456789ABCDEF".IndexOf(char.ToUpper(sourceString[index]));

Or even faster (subtraction vs. array search), but no 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');
}
Ben M
  • 22,262
  • 3
  • 67
  • 71
  • 1
    I ended up with the former, and can now process (rather large) 200k messages/sec as opposed to 180k messeages/sec when using the posted SubString method :) Theres plenty of other Substrings left to eliminate in other ways as well though :-| – nos Aug 03 '09 at 19:16
  • Elegant as hell! Thank you. – Casey Anderson Apr 21 '16 at 13:30
19

Correct me if im wrong but can you simply use

Convert.ToByte(stringValue, 16);

as long as the stringValue represents a hex number? Isnt that the point of the base paramter?

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

almog.ori
  • 7,839
  • 1
  • 35
  • 49
  • as that converts the entire stringValue, and I just need to convert ONE character within the string as mentioned, no I can't use that. – nos Aug 03 '09 at 19:13
  • 1
    stringValue could be one char, its just an example but the point is Strings are immutable. – almog.ori Aug 03 '09 at 19:48
  • Learn something new every day. This is the post that helped me do what I needed. – DaleyKD May 13 '15 at 15:50
2

If you know the hex value is only a byte then just convert to an Int32 and then cast

var b = (byte)(Convert.ToInt32(serializedString, 16));
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • That's obvious, the character will just be the first in a longer string in my case though – nos Jul 31 '09 at 21:37
2

Sure you can get the hex value without ever needing to create another string. I'm not sure what it'll really gain you, performance wise, but since you asked, here's what you've requested.

    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;
            }
        }
    }
Ben Lesh
  • 107,825
  • 47
  • 247
  • 232
2

I am adding another answer just because no one mentioned this one. You can use the built-in Uri.FromHex method for converting a single character:

var b = (byte) System.Uri.FromHex('a'); // b = 10
polfosol ఠ_ఠ
  • 1,840
  • 26
  • 41
-1
Encoding.UTF8.GetBytes( serializedString.ToCharArray(), 0, 1)

Cheaper might be:

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

This will only add the interesting char to the char[] and not the entire string.

Paul van Brenk
  • 7,450
  • 2
  • 33
  • 38