char *pszSerial = "str";
char aChar;
int res = 0;
int cf = 0;
while (aChar = *pszSerial++){
cf = (res & 0xFFFFFFFF) > 0x7FFFFFFF;
res += 0x12345678;
res <<= 1;
res += aChar;
res += cf;
res += 0x87654321;
}
printf("0x%X\n", res);
感谢提醒,确实有问题,改了一下。
char *pszSerial = "12345678910";
char aChar;
unsigned long long res = 0;
while (aChar = *pszSerial++)
{
res += 0x12345678;
res = res << 1 | res >> 32;
res += aChar + (res >> 32 & 1);
res = res + 0x87654321 & 0xffffffff;
}
printf("%08X\n", (unsigned)res);