forked from moov-io/iso8583
-
Notifications
You must be signed in to change notification settings - Fork 0
/
charset.go
31 lines (26 loc) · 809 Bytes
/
charset.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
// Copyright 2020 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package iso8583
import (
"bytes"
"io"
"io/ioutil"
"unicode/utf8"
"golang.org/x/text/encoding/charmap"
"golang.org/x/text/transform"
)
func transformEnconding(reader io.Reader, trans transform.Transformer) ([]byte, error) {
transReader := transform.NewReader(reader, trans)
ret, err := ioutil.ReadAll(transReader)
return ret, err
}
// UTF8ToWindows1252 converts text encoded in UTF-8 to Windows-1252 or CP-1252 encoding
func UTF8ToWindows1252(input []byte) ([]byte, error) {
if utf8.Valid(input) {
reader := bytes.NewReader(input)
res, err := transformEnconding(reader, charmap.Windows1252.NewEncoder())
return res, err
}
return input, nil
}