Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
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:


;# 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"


Posted by admin (Graham Ellis), 10 September 2004
I think this demonstrates what you're looking for ...

set source "Hello World and Tim"
set sofar 0
for {set k 0} {$k < [string length $source]} {incr k} {
binary scan [string index $source $k] c dd
set sofar [expr $dd ^ $sofar]
puts "$dd $sofar"

Which runs as follows:

72 72
101 45
108 65
108 45
111 66
32 98
87 53
111 90
114 40
108 68
100 32
32 0
97 97
110 15
100 107
32 75
84 31
105 118
109 27

Posted by TimT (TimT), 10 September 2004
Your code seems similar to mine apart from being more compact
- but it makes all the difference


This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2015: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: • WEB: • SKYPE: wellho