Does anyone know, how to get the ASCII-Value of a Character,
e.g. Char "A" = 65
Peter
% scan A %c var
1
% set var
65
Pascal
: Does anyone know, how to get the ASCII-Value of a Character,
: e.g. Char "A" = 65
proc get_code {char} {
scan $char "%c" code
return $code
}
%get_code "A"
65
: Peter
--
--------------------------------------------------
Victor Wagner vi...@ice.ru
Programmer Office:7-(095)-333-2022
Institute for Commerce Home: 7-(095)-135-46-61
Engineering http://www.ice.ru/~vitus
that works great until you want to get the code for the
null character (ASCII 0)
% proc get_code {char} {
scan $char "%c" code
return $code
}
% puts [get_code A]
65
% puts [get_code \x01]
1
% puts [get_code \x00]
can't read "code": no such variable
%
I would suggest that this is a bug, since the character really
is there.
% string length "\x00"
1
Evan Rempel
: that works great until you want to get the code for the
: null character (ASCII 0)
: I would suggest that this is a bug, since the character really
: is there.
Of course, this is a bug. But this version would work in Tcl 7.x
for anything but NULLs
for Tcl 8.0
use
proc get_code {char} {
binary scan $char c1 code
return $code
}
you can ever do
if {[info tclversion]>=8.0} {
proc get_code {char} {
binary scan $char c1 a
return $a
}
} else {
proc get_code {char} {
if {[scan $char "%c" a]==1} {
return $a
} else {
return 0
}
}
}
This works for NUL in Tcl 7.5 and Tcl 8.0