-
Notifications
You must be signed in to change notification settings - Fork 0
/
sjis2jis.rb
75 lines (63 loc) · 1.15 KB
/
sjis2jis.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require 'convbase'
# cf. http://www.unixuser.org/~euske/doc/kanjicode/index.html
class Sjis2jis
def conv(instr)
ary = instr.bytes.to_a
outstr = ""
i = 0
kanji = false
while i < ary.length
c = ary[i]
cc = ary[i+1]
if kanji
# kanji
if (0x81 <= c && c <= 0x9f) || (0xe0 <= c && c <= 0xff)
if 0x9f <= cc && cc <= 0xfc
b = 1
cc -= 0x7e
elsif 0x80 <= cc && cc <= 0x9e
b = 0
cc -= 0x20
elsif 0x40 <= cc && cc <= 0x7e
b = 0
cc -= 0x1f
else
raise InvalidInputstringException
end
if 0xe0 <= c
c -= 0xc1
else
c -= 0x81
end
c <<= 1
c += b
c += 0x21
outstr += c.chr
outstr += cc.chr
i += 2
else
outstr += 0x1B.chr
outstr += 0x28.chr
outstr += 0x42.chr
kanji = false
next
end
else
# kanji
if (0x81 <= c && c <= 0x9f) || (0xe0 <= c && c <= 0xff)
outstr += 0x1B.chr
outstr += 0x24.chr
outstr += 0x42.chr
kanji = true
next
else
outstr += c.chr
i += 1
end
end
end
return outstr
end
end
conv = Sjis2jis.new
puts conv.conv("hoge")