Reply To: Need Tcl help: Convert to ulong

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Need Tcl help: Convert to ulong Reply To: Need Tcl help: Convert to ulong

#58828
Rentian Huang
Participant

    John, you are absolutely right, I was being stupid putting that [set sum 0] in the loop, and your format 0x%08x works fine too.

    But somehow I just can not get the correct result. I changed my code as below:

    Code:

    proc sum_add {sum str} {
    set len [string length $str]
    for {set i 0} {$i < $len} {incr i} { set char [string index $str $i] if { ![string is integer $char] } { set char [scan $char %c] } set sum [format 0x%08x [expr ($sum + ($char * (($i + 1) << 4))) % 0x10000]] } return $sum }

    This is the calls:

    Code:

    set sum 0
    set sum [sum_add $sum “”]
    set sum [sum_add $sum “”]
    set sum [sum_add $sum “Dr.X”]
    echo sum_char<$sum>
    set sum [sum_add $sum 1065507963]
    echo sum_num<$sum>

    This is the output:

    Code:

    sum_char<0x00003120>
    sum_num<0x000041f0>

    And according to our vendor, the first one is correct, but the 2nd has problem:

    Code:

    sum in 1st step: 0x00003120
    sum in 2nd step: 0x0000e6f0

    Thanks for any more help, I have been struggling for this for a while   😥

    Sam