######################################################################
# Name: unescape_HL7
# Purpose: Remove HL7 escape sequences and replace them with
# the actual characters represented (including escaped
# hexadecimal values)
# Args: Two strings required
# input – the actual string to be unescaped
# delimiters – the delimiters as foudn in MSH-1 and MSH-2
#
# Comments:
proc unescape_HL7 { input delimiters } {
set delim_f [string range $delimiters 0 0]
set delim_s [string range $delimiters 1 1]
set delim_r [string range $delimiters 2 2]
set delim_e [string range $delimiters 3 3]
set delim_t [string range $delimiters 4 4]
set output “”
while {[ string length $input ]} {
set char [string range $input 0 0]
set input [string range $input 1 end]
#echo processing $char
if { [string equal $char $delim_e] } {
set esc “”
set char [string range $input 0 0]
set input [string range $input 1 end]
while {![string equal $char $delim_e]} {
append esc $char
set char [string range $input 0 0]
set input [string range $input 1 end]
}
#echo “escape sequence: $esc”
switch -exact [string range $esc 0 0] {
“F” {
append output $delim_f
}
“E” {
append output $delim_e
}
“S” {
append output $delim_s
}
“T” {
append output $delim_t
}
“R” {
append output $delim_r
}
“X” {
scan [string range $esc 1 end] “%x” x
append output [format “%c” $x]
}
default {
#echo unknown escape sequence $esc
}
}
} else {
append output $char
}
}
return $output
}