Skip to main content
1 of 4

#Japanese Encoding Conversion

###Background

In JIS X 0208 a 94 by 94 map (kuten) is defined to encode Chinese characters (kanji), phonetic monas (kana) and other symbols. Along with this standard there are 3 different encodings that maintain 1-1 correspondences with the map, namely JIS, Shift_JIS and EUC. The mappings are different that random characters may occur (mojibake) if the wrong encoding is used.

###Conversion Algorithm

In the formulae below, kuten (区点) is a pair of numbers \$(区,点)\$ where \$1\le 区,点\le94\$. The conversions between kuten and the 3 encodings are as follows:

$$\text{JIS}: (a,b)=(20_{16}+区,20_{16}+点)$$

$$\text{Shift_JIS}: (a,b)=\begin{cases} (80_{16}+\lceil\frac{区}{2}\rceil+40_{16}\lfloor\frac{区}{63}\rfloor,\text{3F}_{16}+点+\lfloor\frac{点}{64}\rfloor)&\text{if }区\text{ is odd}\\ (80_{16}+\lceil\frac{区}{2}\rceil+40_{16}\lfloor\frac{区}{63}\rfloor,\text{9E}_{16}+点)&\text{if }区\text{ is even} \end{cases}$$

$$\text{EUC}: (a,b)=(\text{A0}_{16}+区,\text{A0}_{16}+点)$$

###Challenge

Write a program or function that, given a byte pair, the source encoding and the destination encoding, converts the byte pair from the source encoding to the destination encoding and outputs the result. You may assume that the input byte pair represents a valid codepoint in the source encoding.

The input and output formats are flexible and are accepted if reasonable. For example, you may receive or output the byte pair as a list, as two numbers or as a number, either in decimal or hexadecimal; you may also assign a number to each of the possible encodings and use them as the input format of the encodings.

###Sample IO The format used in the samples is SrcCodePoint, SrcEncoding, DstEncoding -> DstCodePoint. Kuten and the corresponding Unicode character are for explanation.

E381, Shift_JIS, EUC -> E5E1 (Kuten: 69-65; U+7E3A 縺)
6562, JIS, Shift_JIS -> E382 (Kuten: 69-66; U+7E67 繧)
E5E3, EUC, JIS       -> 6563 (Kuten: 69-67; U+7E5D 繝)
897E, Shift_JIS, JIS -> 315F (Kuten: 17-63; U+5186 円)
B1E0, EUC, Shift_JIS -> 8980 (Kuten: 17-64; U+5712 園)
3161, JIS, EUC       -> B1E1 (Kuten: 17-65; U+5830 堰)

See here (in Japanese) for the full table.

###Winning condition

This is a code-golf challenge, so the shortest valid submission for each language wins. Standard loopholes are forbidden by default.