Mình có vọc vạch làm keygen cho một soft thì thấy nó dùng thuật toán này, parse chuỗi hex tối đa 4 ký tự, mình chưa hiểu được cơ sở toán học của nó là gì, thấy hay nên share cho mọi người xem:
unsigned __int16 __cdecl HexToDecimal(std::wstring& cStr)
{
int currentWChar; // edx@1
int strLen; // ecx@1
unsigned __int16 result; // ax@2
signed int check; // ecx@3
int encodedWChar; // edx@6
int index = 0;
currentWChar = cStr[0];
strLen = cStr.length();
if (strLen)
{
result = 0;
if (strLen == 4)
{
check = 12;
while (true)
{
currentWChar = cStr[index];
if ((unsigned __int16)currentWChar < 'a' || (unsigned __int16)currentWChar > 'f')
{
if ((unsigned __int16)currentWChar < 'A' || (unsigned __int16)currentWChar > 'F')
{
if ((unsigned __int16)currentWChar < '0' || (unsigned __int16)currentWChar > '9')
return 0;
encodedWChar = currentWChar + 0xFFD0;
}
else
{
encodedWChar = currentWChar + 0xFFC9;
}
}
else
{
encodedWChar = currentWChar + 0xFFA9;
}
currentWChar = encodedWChar << check;
check -= 4;
index++;
result += currentWChar;
if (check <= -4)
return result;
}
}
}
return 0;
}