I need to get the ASCII character for every character in a string. Actually its every character in a (small) file. The following first 3 lines successfully pull all a file's contents into a string (per this recipe):
set fp [open "store_order_create_ddl.sql" r]
set data [read $fp]
close $fp
I believe I am correctly discerning the ASCII code for the characters (see http://wiki.tcl.tk/1497). However I'm having a problem figuring out how to loop over every character in the string.
First of all I don't think the following is an especially idiomatic way of looping over characters in a string with Tcl. Second and more importantly, it behaves incorrectly, inserting an extra element between every character.
Below is the code I've written to act on the contents of the "data" variable set above, followed by some sample output.
CODE:
for {set i 0} {$i < [string length $data]} {incr i} {
set char [string index $data $i]
scan $char %c ascii
puts "char: $char (ascii: $ascii)"
}
OUTPUT:
char: C (ascii: 67)
char: (ascii: 0)
char: R (ascii: 82)
char: (ascii: 0)
char: E (ascii: 69)
char: (ascii: 0)
char: A (ascii: 65)
char: (ascii: 0)
char: T (ascii: 84)
char: (ascii: 0)
char: E (ascii: 69)
char: (ascii: 0)
char: (ascii: 32)
char: (ascii: 0)
char: T (ascii: 84)
char: (ascii: 0)
char: A (ascii: 65)
char: (ascii: 0)
char: B (ascii: 66)
char: (ascii: 0)
char: L (ascii: 76)
char: (ascii: 0)
char: E (ascii: 69)