So, after some time searching and finding that strtol is pretty slow, I encoded my own function. It only works for uppercase letters, but adding lowercase letters is not a problem.
int hexToInt(PCHAR _hex, int offset = 0, int size = 6) { int _result = 0; DWORD _resultPtr = reinterpret_cast<DWORD>(&_result); for(int i=0;i<size;i+=2) { int _multiplierFirstValue = 0, _addonSecondValue = 0; char _firstChar = _hex[offset + i]; if(_firstChar >= 0x30 && _firstChar <= 0x39) _multiplierFirstValue = _firstChar - 0x30; else if(_firstChar >= 0x41 && _firstChar <= 0x46) _multiplierFirstValue = 10 + (_firstChar - 0x41); char _secndChar = _hex[offset + i + 1]; if(_secndChar >= 0x30 && _secndChar <= 0x39) _addonSecondValue = _secndChar - 0x30; else if(_secndChar >= 0x41 && _secndChar <= 0x46) _addonSecondValue = 10 + (_secndChar - 0x41); *(BYTE *)(_resultPtr + (size / 2) - (i / 2) - 1) = (BYTE)(_multiplierFirstValue * 16 + _addonSecondValue); } return _result; }
Using:
char *someHex = "#CCFF00FF"; int hexDevalue = hexToInt(someHex, 1, 8);
1, because the hex that we want to convert starts at offset 1, and 8, because this is the length of the hex.
Speedtest (1,000,000 calls):
strtol ~ 0.4400s hexToInt ~ 0.1100s
mexikanoZ Jan 05 '18 at 17:32 2018-01-05 17:32
source share