Originally posted by Rentian Huang
I have a C function that generates a key, and needs to be converted into a Tcl function:
void sum_add(ulong*sum, const char* str)
{
size_t len = strlen(str);
for (size_t i=0; i<len; i++) {
*sum = (*sum + ((ulong)str[i]*((i+1)<<4)))) % 0x10000;
}
}
This is what I did:
proc sum_add {sum str} {
set len [string length $str]
for {set i 0} {$i < $len} {incr i} {
set sum 0
set sum [expr ($sum + ([scan [string index $str $i] %c] * (($i + 1) << 4))) % 0x10000]
}
return $sum
}ulong is a 32-bit unsigned integer
The correct result is: [sum_add 0 "Dr.X"] = 0x00003120
But I got [sum_add 0 "Dr.X"] = 5632
Why? I guess the scan has problem but I don't know how to fix it.
Can anyone help? Thanks!
Sam 8)