Question: v2.x delimiter escape sequences

May 21, 2014

Question:

Is there a good, public algorithm for HL7 v2.x delimiter escape sequences?

Answer:

Well, yes, and no. The various open source libraries for v2 all include escape sequences - I wrote some of them myself. The syntactical escape sequences are easy - simply replace the contents of the data element component the escape sequence - so that’s trivial:

StringBuilder b = new StringBuilder();
for char in content  {
  switch (char) {
    case FieldDel : b.append(EscapeChar + 'F' + EscapeChar); // |
    case ComponentDel: b.append(EscapeChar + 'S' + EscapeChar); // ^
    case SubComponentDel: b.append(EscapeChar + 'T' + EscapeChar); // &
    case RepetitionDel : b.append(EscapeChar + 'R' + EscapeChar); // ~
    case EscapeChar : b.append(EscapeChar + 'E' + EscapeChar); // ~
    case TruncationChar : b.append(EscapeChar + 'P' + EscapeChar); // # (v2.7+)
    default: if (char in [#10, #13, #9] || char.toInteger >= 128)
        b.append(EscapeChar + 'X' + char.toInteger.ToHex + Escape;
      else 
        b.append(char)
  }
}
return b.toString();

Note: that pseudo code is mish mash of java, C#, and pascal.  Note that the values of the delimiter characters are configurable, so they’re constants, not hard coded, though many implementation guides fix them to the default values shown.

However, there’s a problem: there are different kinds of escape sequences:

  • syntactical
  • highlighting
  • character set escapes
  • binary escapes

These have different implications depending on your architecture, so it’s not really possible to have “an algorithm for escape sequences”. Instead, you have to decide what you need to do about these