-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversion.go
49 lines (40 loc) · 1.08 KB
/
conversion.go
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
package ebcdic
import (
"errors"
)
// ToUnicode converts ebcdic data to unicode data
func ToUnicode(ebcdicData []byte, codePage CodePage) ([]rune, error) {
conv, ok := codeTables[codePage]
if !ok {
return nil, errors.New("code page not supported")
}
unicodeData := make([]rune, len(ebcdicData))
for i, ebcdicDatum := range ebcdicData {
switch {
case conv.EuroChar != 0 && ebcdicDatum == conv.EuroChar:
unicodeData[i] = 0x20AC
default:
unicodeData[i] = conv.ToUnicode[ebcdicDatum]
}
}
return unicodeData, nil
}
// FromUnicode converts unicode data to ebcdic data
func FromUnicode(unicodeData []rune, codePage CodePage) ([]byte, error) {
conv, ok := codeTables[codePage]
if !ok {
return nil, errors.New("code page not supported")
}
ebcdicData := make([]byte, len(unicodeData))
for i, unicodeDatum := range unicodeData {
switch {
case conv.EuroChar != 0 && unicodeDatum == 0x20AC:
ebcdicData[i] = conv.EuroChar
case unicodeDatum <= 0xFF:
ebcdicData[i] = conv.FromUnicode[unicodeDatum]
default:
ebcdicData[i] = 0x00
}
}
return ebcdicData, nil
}