title: Which encoding? Character strings enclosed by tilde ~ and curly braces {}
body: <p>The APNIC Whois database contains a lot of entries for Chinese entities with some sort of encoding enclosed by <code>~{</code> and <code>~}</code>. For example:</p>
  
  <pre><code>$ whois 211.68.92.0 | grep ^descr:
  descr:          ~{146{J5QiJR;y4!?FQ'QP&gt;?T:~}(~{VP9z~})
  descr:          Bell Labs Research China
  descr:          Beijing 100080, China
  </code></pre>
  
  <p>Anyone know what this is? An encoding of some sort? My first guess was Punycode but quickly realised that it wouldn't include some of the special characters that are in there.</p>
  
  <p>I also found this encoding on some web pages, like <a href="https://www.sony.com.cn/products/mv/cs.htm" rel="nofollow noreferrer">that</a>.</p>
  
  <p>Would be interesting to decode this, out of curiosity.</p>
  
  <p><strong>EDIT: Found in RFC 1842.</strong></p>
  
  <blockquote>
    <p>For an arbitrary mixed text with both Chinese coded text strings and
    ASCII text strings, we designate to two distinguishable text modes,
    ASCII mode and HZ mode, as the only two states allowed in the text. At
    any given time, the text is in either one of these two modes or in the
    transition from one to the other. In the HZ mode, only printable ASCII
    characters (0x21-0x7E) are meanful with the size of basic text unit
    being two bytes long.</p>
    
    <p>In the ASCII mode, the size of basic text unit is one (1) byte with
    the exception '~~', which is the special sequence representing the
    ASCII character '~'. In both ASCII mode and HZ mode, '~' leads an
    escape sequence. However, as HZ mode has basic size of text unit being
    2 bytes long, only the '~' character which appears at the first byte
    of the the two-byte character frame are considered as the start of an
    escape sequence.</p>
    
    <p>The default mode is ASCII mode. Each line of text starts with the
    default ASCII mode. Therefore, all Chinese character strings are to be
    enclosed with '~{' and '~}' pair in the same text line. </p>
    
    <p>The escape sequences defined are as the following:</p>
  
  <pre><code>~{       ---- escape from ASCII mode to GB2312 HZ mode
  ~}       ---- escape from HZ mode to ASCII mode
  ~~       ---- ASCII character '~' in ASCII mode
  ~\n      ---- line continuation in ASCII mode
  ~[!-z|]  ---- reserved for future HZ mode character sets
  </code></pre>
    
    <p>A few examples of the 7 bit representation of Chinese GB coded test
    taken directly from [Lee89] are listed as the following:</p>
    
    <p>Example 1:  (Suppose there is no line size limit.)
               This sentence is in ASCII.
               The next sentence is in GB.~{&lt;:Ky2;S{#,NpJ)l6HK!#~}Bye.</p>
    
    <p>Example 2:  (Suppose the maximum line size is 42.)
               This sentence is in ASCII.
               The next sentence is in GB.~{&lt;:Ky2;S{#,~}~
               ~{NpJ)l6HK!#~}Bye.</p>
    
    <p>Example 3:  (Suppose a new line is started for every mode switch.)
               This sentence is in ASCII.
               The next sentence is in GB.~
               ~{&lt;:Ky2;S{#,NpJ)l6HK!#~}~
               Bye.</p>
  </blockquote>
  
  <p>How would I go about decoding this in python3?</p>