4

I am using ca.uhn.hl7v2.util.Terser to create a HL7 message. For one of the HL7 fields I need to set the following value "\home\one\two".

HL7 message type is MDM_T02(version is 2.3.1). Because "" is an escape character in hl7 messages if I try to use

public void methodOne() {
   MDM_T02 mdmt02  = new MDM_T02();
   Terser terser = new Terser(mdmt02);
   terser.set("OBX-5-1", "\\\\usne-server\\Pathology\\Quantum"); 
}

In the HL7 message OBX-5-1 is printed as "\E\E\usne-server\E\Pathology\E\Quantum".

Can someone help me to print the proper message?

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
  • Is the collapsing of escape characters intentional in `\E\E\...` or should that be `\E\\E\...` ? – BenW Apr 08 '22 at 15:24

2 Answers2

8

You may refer to the description of HL7 Escape Sequences here or here.

HL7 defines character sequences to represent ’special’ characters not otherwise permitted in HL7 messages. These sequences begin and end with the message’s Escape character (usually ‘\’), and contain an identifying character, followed by 0 or more characters. The most common use of HL7 escape sequences is to escape the HL7 defined delimiter characters.

Character     Description Conversion
\Cxxyy\       Single-byte character set escape sequence with two hexadecimal values not converted
\E\           Escape character converted to escape character (e.g., ‘\’)
\F\           Field separator converted to field separator character (e.g., ‘|’)
\H\           Start highlighting not converted
\Mxxyyzz\     Multi-byte character set escape sequence with two or three hexadecimal values (zz is optional) not converted
\N\           Normal text (end highlighting) not converted
\R\           Repetition separator converted to repetition separator character (e.g., ‘~’)
\S\           Component separator converted to component separator character (e.g., ‘^’)
\T\           Subcomponent separator converted to subcomponent separator character (e.g., ‘&’)
\Xdd…\        Hexadecimal data (dd must be hexadecimal characters) converted to the characters identified by each pair of digits
\Zdd…\        Locally defined escape sequence not converted

If \ is part of your data, you need to escape it with \E\.

So your value:

"\home\one\two"

becomes

"\E\home\E\one\E\two"

About second issue:

In the HL7 message OBX-5-1 is printed as "\E\E\usne-server\E\Pathology\E\Quantum".

While reading the value, you have to reverse the process. That means, you should replace \E\ with \ back to get original value.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
1

As @Amit Joshi mentioned, this has to do with HL7 escaping. You may want to try to change your escape character to one other than a backslash that is unlikely to appear in your message as your client appears to not be following it anyway.

This would be the 3rd character in MSH-2.

agermano
  • 1,679
  • 6
  • 16
  • 1
    Nice point. But in that case, that new character should not be the part of value and should be escaped same as mentioned in my answer. +1. – Amit Joshi Nov 21 '18 at 05:49
  • 1
    While this is valid according to the HL7 spec, a surprising number of healthcare systems don't support changing the control characters. – Kramii Oct 16 '20 at 07:52
  • If you pay close attention to the original post, the library is automatically doing the escaping, and the downstream system is not unescaping, thus my suggestion to change the escape character to something unlikely to appear in the message. It will get escaped by the library, so no worries there, but downstream appears to already be ignoring it, so it won't hurt to change it. – agermano Oct 19 '20 at 13:51