views:

2055

answers:

7

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?

A: 

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
That's obvious, the character will just be the first in a longer string in my case though
nos
+1  A: 

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
You're likely right. It just seems like a big waste creating > 120000 of these small objects per second here
nos
A: 
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.

pb
The OP is worried about creating a new string. This solution will create a new array.
JaredPar
This will get the UTF8 byte of the character... not the value of the character as hex.
blesh
+4  A: 
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
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
+5  A: 

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
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
stringValue could be one char, its just an example but the point is Strings are immutable.
almog.ori
A: 

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;
            }
        }
    }
blesh