RFC 1843
HZ - A Data Format for Exchanging Files of Arbitrarily Mixed Chinese and ASCII characters
RFC 1843 π HZ - A Data Format for Exchanging Files of Arbitrarily Mixed Chinese and ASCII characters
Okay bestie, this one is from F. Lee at Stanford University π. It's an Informational doc from August 1995, so it's giving info and not a formal Internet standard π. The title block doesn't list anything it obsoletes or updates, so it's a solo icon with no drama π .
Status of This Memo πΈ
This memo gives info to the Internet community β¨. It does not specify an Internet standard of any kind, so no pressure π. Distribution of this memo is unlimited π¦.
Abstract π
Okay so this is basically the same as an article with the same title that the author wrote on September 4, 1989 π. In this memo, GB means GB2312-80 π¨π³. The title is only kept for historical reasons, and HZ has been used way more for stuff beyond "file exchange" than the name suggests π.
1. Introduction β¨
Most computer systems that can handle a text file with arbitrarily mixed Chinese and ASCII characters use 8-bit codes π». So if you want to send those files through email on ASCII systems, you have to encode them into a 7-bit format π¨. A generic binary-to-ASCII encoder isn't enough, though, because there's no universal standard for those 8-bit codes yet π. CCDOS and the Macintosh's Chinese OS, for example, use different internal codes π. The good news is that there's a PRC national standard, GuoBiao (GB), for encoding Chinese characters, and characters from those systems can be easily converted to GB with a simple formula π«Ά. (* The ROC standard BIG-5 is outside the scope of this article.)
HZ is a 7-bit data format built for exchanging text files that mix GB and ASCII π. It's also meant for designing terminal emulators that show and edit mixed Chinese and ASCII text files in real time π₯οΈβ¨.
2. Specification π
Here's how HZ works π.
Without loss of generality, we assume all Chinese characters (HanZi) have already been encoded in GB π¨π³. A GB code (GB1 and GB2) is a two byte code. The first byte is in the range $21-$77 (hexadecimal), and the second byte is in the range $21-$7E π’.
A graphical ASCII character is a byte in the range $21-$7E π€. A non-graphical ASCII character is a byte in the range $0-$20 or the value $7F π.
The range of a graphical ASCII character overlaps with a GB byte, so a byte in $21-$7E gets interpreted based on which mode you're in π§ . There are two modes: ASCII mode and GB mode π.
By convention, a non-graphical ASCII character should only appear in ASCII mode π.
The default mode is ASCII mode π .
In ASCII mode, a byte is read as an ASCII character unless you run into a ~, which is the escape character π. By convention, it must be immediately followed ONLY by ~, { or \n (<LF>), and each one has a special meaning:
~~is interpreted as a~π«.- The escape-to-GB sequence
~{switches the mode from ASCII to GB π. - The escape sequence
~\nis a line-continuation marker. It gets consumed and produces no output π«§.
In GB mode, characters are read two bytes at a time as (pure) GB codes until the escape-from-GB code ~} shows up π§©. That code switches the mode from GB back to ASCII π. (Note that the escape-from-GB code ~} ($7E7D) is outside the defined GB range π.)
The decoding process is clear from the above π«Ά.
Encoding is pretty straightforward too π°. An (ASCII) ~ is always encoded as ~~ π. A sequence of GB codes gets wrapped in ~{ and ~} π.
3. Remarks & Recommendations π
We choose to encode any ASCII character except ~ as-is, rather than as a two byte code. We also pick ASCII as the default mode, and here's why π. The computers we used were ASCII based π». A HZ file with pure ASCII characters (i.e. no Chinese characters) except for ~ is exactly a pure ASCII file π. Overall, the English (ASCII) part of a HZ file is directly readable πβ¨.
The escape character ~ was picked because it's commonly used in the ASCII world π. It's also outside the defined range ($21-$77) of the first byte of a GB code, since ~ ($7E) sits outside that range π§.
In ASCII mode, any other two byte sequences that start with ~ (other than ~~, ~{ and ~\n) are currently invalid HZ sequences π«. That means they can be used to extend HZ later with total upward compatibility π¦.
The line-continuation marker ~\n is super handy if you want to split long lines of the original text into shorter lines in this format, without adding extra newline characters when decoding π.
There's no limit on the length of a line π. The whole file could even be one long line, or have no newline characters at all π€―. A decoder for this HZ format shouldn't, and doesn't need to, think in terms of lines π ββοΈ.
Encoders and decoders for HZ are easy to write π§. An encoder or decoder only needs to look ahead at most one character in the input stream π.
Given the current mode, you can also decode a HZ data stream by scanning backward, which is easy and totally doable π. One implication is that "backspaces" can be handled correctly by a terminal emulator β«π .
To make it easier to use programs that support line and page skips, like "more" on UNIX, with a terminal emulator that understands HZ, it is RECOMMENDED that the ENCODER (which outputs HZ) sets a maximum line size of less than 80 characters π. Since \n is an ASCII character, the HZ syntax then automatically means that GB codes at the end of a line must be closed with the escape-from-GB code ~}, and the line-continuation marker ~\n should be inserted where it fits π§΅. The tradeoff is that the encoded file is a little bigger π¦.
Now, this part is important π¨. The recommendation above does NOT change the HZ format at all π ββοΈ. It's just an encoding "style" that follows the HZ syntax π¨. That style isn't built into HZ; it's an extra convention layered "on top of" HZ π°. Other applications may need different "styles", but the same basic HZ DECODER will always work π―. The whole point of HZ is to be a flexible basic data format for files that mix Chinese and ASCII characters, and that's exactly what it does πβ¨.
4. Examples π§
To show the "stylistic" side of HZ encoding, here are four examples of encoded text that should all decode to the same output. (The recommendation in the last section refers to Example 2 π .)
Heads up, bestie: the original says four examples but only three actually show up below π. I'm just flagging it so you're not confused.
Example 1: (Suppose there is no line size limit.)
This sentence is in ASCII.
The next sentence is in GB.~{<:Ky2;S{#,NpJ)l6HK!#~}Bye.
Example 2: (Suppose the maximum line size is 42.)
This sentence is in ASCII.
The next sentence is in GB.~{<:Ky2;S{#,~}~
~{NpJ)l6HK!#~}Bye.
Example 3: (Suppose a new line is started whenever there is a mode switch.)
This sentence is in ASCII.
The next sentence is in GB.~
~{<:Ky2;S{#,NpJ)l6HK!#~}~
Bye.
Acknowledgement π«Ά
Edmund Lai was the first person to bring this topic to the author's attention π. Discussions with Ed, Tin-Fook Ngai, Yagui Wei and Ricky Yeung were super helpful in shaping the ideas in this article π. A big thank you to Tin-Fook for his careful review of the draft and his many interesting suggestions πΈ.
References π
[1] Fung Fung Lee, "HZ - A Data Format for Exchanging Files of Arbitrarily Mixed Chinese and ASCII Characters," September 4, 1989. As part of //ftp.ifcss.org/software/unix/convert/HZ-2.0.tar.gz
Security Considerations π₯
Security issues are not addressed in this memo. π Honestly, that's a big deal, so don't assume this doc covers it.
Author's Address π
- Fung Fung Lee
- Computer Systems Laboratory
- Stanford University
- Stanford, CA 94309
- Phone: +1 415 723 1450
- EMail: lee@csl.stanford.edu
π¦ where this page comes from
- ietf: rfc1843 1995-08
- π girlified by claude-haiku-5-5 on : a language model's rewrite of the original, under its terms