0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

シフト JIS コードは 0x2000 / 0xA000 と XOR して扱う

Posted at

SHIFT JIS の領域 (CP932) の上位4ビットを見ると

上位4ビット 8 9 A B C D E F
S-JIS

8,9,E,F が対象で領域が分離されています。

しかし、XOR(2 または A)すると、(8,9)と(A,B) および (C,D)と(E,F) が交換されて

S-JIS
XOR 2 A → 8 B → 9 8 → A 9 → B E → C F → D C → E D → F
XOR A A → 0 B → 1 8 → 2 9 → 3 E → 4 F → 5 C → 6 D → 7

シフトJIS の範囲を連続的(A-D または 2-5)に扱えます。

最初の1バイトがS-JISか?

int is_sjis(int first_byte)
{
  return ((unsigned)((first_byte ^ 0xa0) - 0x21) < 0x3c); /* CP932の範囲 */
}

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?