An easier method might be something like:
Assume the string is in a variable called str
if {[regsub -all — {[^x00-x7F]} $str
{\x0[format %02x [scan & %c]]} tmp]} {
set str [subst $tmp]
}
If the string has non-ascii characters they get converted, else not