Skip to main content
Commonmark migration
Source Link

###Background

Background

In JIS X 0208 a 94 by 94 map (kuten (区点)) is defined to encode Chinese characters (kanji), phonetic moras (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. These encodings take 2 bytes for each character in the kuten map. The mappings are different that random characters may occur (mojibake) if the wrong encoding is used.

###Conversion Algorithm

Conversion Algorithm

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

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

$$\text{Shift_JIS}: (a,b)=\begin{cases} \left(128+\left\lceil\frac{区}{2}\right\rceil+64\left\lfloor\frac{区}{63}\right\rfloor,63+点+\left\lfloor\frac{点}{64}\right\rfloor\right)&\text{if }区\text{ is odd}\\ \left(128+\left\lceil\frac{区}{2}\right\rceil+64\left\lfloor\frac{区}{63}\right\rfloor,158+点\right)&\text{if }区\text{ is even} \end{cases}$$

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

###Challenge

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

###Sample IO TheThe format used in the samples is SrcCodePoint, SrcEncoding, DstEncoding -> DstCodePoint. Decimal is used here but hexadecimal is also acceptable. The Unicode character and its assigned kuten are for explanation.

(227, 129), Shift_JIS, EUC -> (229, 225) (Kuten: 69-65; U+7E3A 縺)
(101, 98), JIS, Shift_JIS  -> (227, 130) (Kuten: 69-66; U+7E67 繧)
(229, 227), EUC, JIS       -> (101, 99)  (Kuten: 69-67; U+7E5D 繝)
(137, 126), Shift_JIS, JIS -> (49, 95)   (Kuten: 17-63; U+5186 円)
(177, 224), EUC, Shift_JIS -> (137, 128) (Kuten: 17-64; U+5712 園)
(49, 97), JIS, EUC         -> (177, 225) (Kuten: 17-65; U+5830 堰)

See here (in Japanese) for the full table.

###Winning condition

Winning condition

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

###Background

In JIS X 0208 a 94 by 94 map (kuten (区点)) is defined to encode Chinese characters (kanji), phonetic moras (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. These encodings take 2 bytes for each character in the kuten map. The mappings are different that random characters may occur (mojibake) if the wrong encoding is used.

###Conversion Algorithm

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

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

$$\text{Shift_JIS}: (a,b)=\begin{cases} \left(128+\left\lceil\frac{区}{2}\right\rceil+64\left\lfloor\frac{区}{63}\right\rfloor,63+点+\left\lfloor\frac{点}{64}\right\rfloor\right)&\text{if }区\text{ is odd}\\ \left(128+\left\lceil\frac{区}{2}\right\rceil+64\left\lfloor\frac{区}{63}\right\rfloor,158+点\right)&\text{if }区\text{ is even} \end{cases}$$

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

###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. Decimal is used here but hexadecimal is also acceptable. The Unicode character and its assigned kuten are for explanation.

(227, 129), Shift_JIS, EUC -> (229, 225) (Kuten: 69-65; U+7E3A 縺)
(101, 98), JIS, Shift_JIS  -> (227, 130) (Kuten: 69-66; U+7E67 繧)
(229, 227), EUC, JIS       -> (101, 99)  (Kuten: 69-67; U+7E5D 繝)
(137, 126), Shift_JIS, JIS -> (49, 95)   (Kuten: 17-63; U+5186 円)
(177, 224), EUC, Shift_JIS -> (137, 128) (Kuten: 17-64; U+5712 園)
(49, 97), JIS, EUC         -> (177, 225) (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.

Background

In JIS X 0208 a 94 by 94 map (kuten (区点)) is defined to encode Chinese characters (kanji), phonetic moras (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. These encodings take 2 bytes for each character in the kuten map. The mappings are different that random characters may occur (mojibake) if the wrong encoding is used.

Conversion Algorithm

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

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

$$\text{Shift_JIS}: (a,b)=\begin{cases} \left(128+\left\lceil\frac{区}{2}\right\rceil+64\left\lfloor\frac{区}{63}\right\rfloor,63+点+\left\lfloor\frac{点}{64}\right\rfloor\right)&\text{if }区\text{ is odd}\\ \left(128+\left\lceil\frac{区}{2}\right\rceil+64\left\lfloor\frac{区}{63}\right\rfloor,158+点\right)&\text{if }区\text{ is even} \end{cases}$$

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

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. Decimal is used here but hexadecimal is also acceptable. The Unicode character and its assigned kuten are for explanation.

(227, 129), Shift_JIS, EUC -> (229, 225) (Kuten: 69-65; U+7E3A 縺)
(101, 98), JIS, Shift_JIS  -> (227, 130) (Kuten: 69-66; U+7E67 繧)
(229, 227), EUC, JIS       -> (101, 99)  (Kuten: 69-67; U+7E5D 繝)
(137, 126), Shift_JIS, JIS -> (49, 95)   (Kuten: 17-63; U+5186 円)
(177, 224), EUC, Shift_JIS -> (137, 128) (Kuten: 17-64; U+5712 園)
(49, 97), JIS, EUC         -> (177, 225) (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.

Became Hot Network Question
Tweeted twitter.com/StackCodeGolf/status/1236939901704298496
deleted 44 characters in body
Source Link
Shieru Asakoto
  • 6.3k
  • 16
  • 40

###Background

In JIS X 0208 a 94 by 94 map (kuten (区点)) is defined to encode Chinese characters (kanji), phonetic moras (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. These encodings take 2 bytes for each character in the kuten map. The mappings are different that random characters may occur (mojibake) if the wrong encoding is used.

###Conversion Algorithm

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

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

$$\text{Shift_JIS}: (a,b)=\begin{cases} \left(128+\left\lceil\frac{区}{2}\right\rceil+64\left\lfloor\frac{区}{63}\right\rfloor,63+点+\left\lfloor\frac{点}{64}\right\rfloor\right)&\text{if }区\text{ is odd}\\ \left(128+\left\lceil\frac{区}{2}\right\rceil+64\left\lfloor\frac{区}{63}\right\rfloor,158+点\right)&\text{if }区\text{ is even} \end{cases}$$

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

###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. Decimal is used here but hexadecimal is also acceptable. The Unicode character and its assigned kuten are for explanation.

(227, 129), Shift_JIS, EUC -> (229, 225) (Kuten: 69-65; U+7E3A 縺)
(101, 98), JIS, Shift_JIS  -> (227, 130) (Kuten: 69-66; U+7E67 繧)
(229, 227), EUC, JIS       -> (101, 99)  (Kuten: 69-67; U+7E5D 繝)
(137, 126), Shift_JIS, JIS -> (49, 95)   (Kuten: 17-63; U+5186 円)
(177, 224), EUC, Shift_JIS -> (137, 128) (Kuten: 17-64; U+5712 園)
(49, 97), JIS, EUC         -> (177, 225) (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.

###Background

In JIS X 0208 a 94 by 94 map (kuten (区点)) is defined to encode Chinese characters (kanji), phonetic moras (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. These encodings take 2 bytes for each character in the kuten map. 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\$, and \$(a,b)\$ is a pair of bytes where \$0\le(a,b)\le 255\$. The conversions between kuten and the 3 encodings are as follows:

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

$$\text{Shift_JIS}: (a,b)=\begin{cases} \left(128+\left\lceil\frac{区}{2}\right\rceil+64\left\lfloor\frac{区}{63}\right\rfloor,63+点+\left\lfloor\frac{点}{64}\right\rfloor\right)&\text{if }区\text{ is odd}\\ \left(128+\left\lceil\frac{区}{2}\right\rceil+64\left\lfloor\frac{区}{63}\right\rfloor,158+点\right)&\text{if }区\text{ is even} \end{cases}$$

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

###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. Decimal is used here but hexadecimal is also acceptable. The Unicode character and its assigned kuten are for explanation.

(227, 129), Shift_JIS, EUC -> (229, 225) (Kuten: 69-65; U+7E3A 縺)
(101, 98), JIS, Shift_JIS  -> (227, 130) (Kuten: 69-66; U+7E67 繧)
(229, 227), EUC, JIS       -> (101, 99)  (Kuten: 69-67; U+7E5D 繝)
(137, 126), Shift_JIS, JIS -> (49, 95)   (Kuten: 17-63; U+5186 円)
(177, 224), EUC, Shift_JIS -> (137, 128) (Kuten: 17-64; U+5712 園)
(49, 97), JIS, EUC         -> (177, 225) (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.

###Background

In JIS X 0208 a 94 by 94 map (kuten (区点)) is defined to encode Chinese characters (kanji), phonetic moras (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. These encodings take 2 bytes for each character in the kuten map. The mappings are different that random characters may occur (mojibake) if the wrong encoding is used.

###Conversion Algorithm

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

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

$$\text{Shift_JIS}: (a,b)=\begin{cases} \left(128+\left\lceil\frac{区}{2}\right\rceil+64\left\lfloor\frac{区}{63}\right\rfloor,63+点+\left\lfloor\frac{点}{64}\right\rfloor\right)&\text{if }区\text{ is odd}\\ \left(128+\left\lceil\frac{区}{2}\right\rceil+64\left\lfloor\frac{区}{63}\right\rfloor,158+点\right)&\text{if }区\text{ is even} \end{cases}$$

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

###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. Decimal is used here but hexadecimal is also acceptable. The Unicode character and its assigned kuten are for explanation.

(227, 129), Shift_JIS, EUC -> (229, 225) (Kuten: 69-65; U+7E3A 縺)
(101, 98), JIS, Shift_JIS  -> (227, 130) (Kuten: 69-66; U+7E67 繧)
(229, 227), EUC, JIS       -> (101, 99)  (Kuten: 69-67; U+7E5D 繝)
(137, 126), Shift_JIS, JIS -> (49, 95)   (Kuten: 17-63; U+5186 円)
(177, 224), EUC, Shift_JIS -> (137, 128) (Kuten: 17-64; U+5712 園)
(49, 97), JIS, EUC         -> (177, 225) (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.

Source Link
Shieru Asakoto
  • 6.3k
  • 16
  • 40

Japanese Encoding Conversion

###Background

In JIS X 0208 a 94 by 94 map (kuten (区点)) is defined to encode Chinese characters (kanji), phonetic moras (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. These encodings take 2 bytes for each character in the kuten map. 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\$, and \$(a,b)\$ is a pair of bytes where \$0\le(a,b)\le 255\$. The conversions between kuten and the 3 encodings are as follows:

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

$$\text{Shift_JIS}: (a,b)=\begin{cases} \left(128+\left\lceil\frac{区}{2}\right\rceil+64\left\lfloor\frac{区}{63}\right\rfloor,63+点+\left\lfloor\frac{点}{64}\right\rfloor\right)&\text{if }区\text{ is odd}\\ \left(128+\left\lceil\frac{区}{2}\right\rceil+64\left\lfloor\frac{区}{63}\right\rfloor,158+点\right)&\text{if }区\text{ is even} \end{cases}$$

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

###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. Decimal is used here but hexadecimal is also acceptable. The Unicode character and its assigned kuten are for explanation.

(227, 129), Shift_JIS, EUC -> (229, 225) (Kuten: 69-65; U+7E3A 縺)
(101, 98), JIS, Shift_JIS  -> (227, 130) (Kuten: 69-66; U+7E67 繧)
(229, 227), EUC, JIS       -> (101, 99)  (Kuten: 69-67; U+7E5D 繝)
(137, 126), Shift_JIS, JIS -> (49, 95)   (Kuten: 17-63; U+5186 円)
(177, 224), EUC, Shift_JIS -> (137, 128) (Kuten: 17-64; U+5712 園)
(49, 97), JIS, EUC         -> (177, 225) (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.