Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I get ASCI code off letters?

Status
Not open for further replies.

czarek

Technical User
Dec 8, 2002
3
PL
How can I get ASCI code off letters?
a-z, A-Z ???

Help me please!!!!!!!!!!!!!!!!!
 
You can use the scan command for that:
scan "a" %c a_ascii
puts $a_ascii

From the manual:
c
A single character is read in and its binary value is stored in the variable as a decimal string. Initial white space is not skipped in this case, so the input field may be a white-space character. This conversion is different from the ANSI standard in that the input field always consists of a single character and no field width may be specified.

HTH
ulis
 
A what when I need hexadecimal format Asci code of letters?
 
proc hextrans {val {a "0"} {d "0"} {h "0"}} {
array set transtable {}
createtable transtable "ABCDEF" "ABCDEF"
createtable transtable "0123456789" "0123456789"
createtable transtable "0123456789" "ABCDEF"
createtable transtable "ABCDEF" "0123456789"

if {$a > 0} {
foreach id [array name transtable] {
set hx [lindex [split $id ","] 0]
if {[string compare $transtable($id) $val] == 0} {
regsub -all "\"" $hx "" hx
set dec [format %d $hx]
return [list hex=$hx dec=$dec]
}
}
}

if {$d > 0} {
foreach id [array name transtable] {
set hx [lindex [split $id ","] 0]
if {[string compare $transtable($id) $val] == 0} {
regsub -all "\"" $hx "" hx
set cha [format %c $hx]
return [list hex=$hx ascii=$cha]
}
}
}

set rlist {}
if {$h > 0} {
foreach id [array name transtable] {
set hx [lindex [split $id ","] 0]
regsub -all "\"" $hx "" hx
if {[string compare $hx $val] == 0} {
lappend rlist $transtable($id)
}
}
return $rlist
}

return
}

proc createtable {arr list1 list2} {
upvar 1 $arr loc
set cnt [array size loc]
foreach val [split $list1 ""] {
foreach out [split $list2 ""] {
set loc("0x$val$out",[incr cnt]) [format %c "0x$val$out"]
set loc("0x$val$out",[incr cnt]) [format %d "0x$val$out"]
}
}
return
}

hextrans A 1 0 0
0x41 65
hextrans 65 0 1 0
0x41 A
hextrans 0x41 0 0 1
A 65

Two years old code, so it's ugly, but it works alright.
HTH.
 
Here is a compact solution:

proc hexacode {letter} {
scan $letter %c ascii
return 0x[format %X $ascii]
}
puts [hexacode A]
->0x41
puts [hexacode z]
->0x7A

HTH

ulis
 
Did You know how to do it in "back side" from asci code to letter?
 
Yep!

proc hexacode {letter} {
scan $letter %c a_ascii
return 0x[format %X $a_ascii]
}
set code_A [hexacode A]
set code_z [hexacode z]
puts [format %c $code_A]
puts [format %c $code_z]

HTH

ulis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top