| |||||||||||
Generating checksum - processing at byte level Posted by TimT (TimT), 9 September 2004 I am trying to calcuate a checksum on a string- XORing each character in turn But after the first XOR the value is a string and not a byte so it fails to continue How do I keep the XORed result as a byte (or in binary)? the code is as follows - defaults to wish but really a tcl program: #!usr/bin/wish ;# CheckSum calculate: ;# XOR each byte with the previous ;# ie XOR byte 1 and 2 then XOR the result with the next byte ;# and repeat until all bytes have been XOR ;# the FCS is the value left set tstrg "ABCDE" set len [string length $tstrg] set fcs [string index $tstrg 0] puts "\nstring test: $tstrg and first character is $fcs\n" for {set n 1} {$n < $len} {incr n} { set indx [string index $tstrg $n] puts "$fcs and $indx are XORed to:" binary scan $fcs c bfcs binary scan $indx c bstrg set fcs [expr $bfcs ^ $bstrg] ;# XOR binarys: bfcs bstrg ;# BUT the above line sets fcs to a string of the value ;# NOT the binary or as a single byte ;# so from next loop on it is wrong ;# *** how do you keep (or convert) the fcs value as a single binary byte? puts "$bfcs, $bstrg goto $fcs" } puts "FCS is $fcs" exit Posted by admin (Graham Ellis), 10 September 2004 I think this demonstrates what you're looking for ...Code:
Which runs as follows: Code:
Posted by TimT (TimT), 10 September 2004 Your code seems similar to mine apart from being more compact- but it makes all the difference Thanks Tim This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |