2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
32 func (bcd *bcdbase) index(c byte) int {
33 for cpos, cchar := range bcd.convtbl {
41 func (bcd *bcdbase) byte(i int) byte {
48 //-----------------------------------------------------------------------------
51 // String format : D1D2D3D4D5
52 // BCD Coded format: 0xD1D2 0xD3D4 0xD50f
53 // String format : D1D2D3D4D5F
56 // String format : D1D2D3D4D5D6
57 // BCD Coded format: 0xD1D2 0xD3D4 0xD5D6
58 // String format : D1D2D3D4D5D6
60 //-----------------------------------------------------------------------------
65 func (bcd *Bcd) Encode(str string) []byte {
66 buf := make([]byte, len(str)/2+len(str)%2)
67 for i := 0; i < len(str); i++ {
68 var schar int = bcd.index(str[i])
74 buf[i/2] |= ((uint8)(schar) & 0x0f)
76 buf[i/2] = 0x0f | (uint8)(schar)<<4
83 func (bcd *Bcd) Decode(buf []byte) string {
85 for i := 0; i < len(buf); i++ {
88 b = bcd.byte(int(buf[i] >> 4))
92 strbytes = append(strbytes, b)
94 b = bcd.byte(int(buf[i] & 0x0f))
98 strbytes = append(strbytes, b)
100 return string(strbytes)
103 //-----------------------------------------------------------------------------
106 // String format : D1D2D3D4D5
107 // TBCD Coded format: 0xD2D1 0xD4D3 0x0fD5
108 // String format : D1D2D3D4D5F
111 // String format : D1D2D3D4D5
112 // TBCD Coded format: 0xD2D1 0xD4D3 0xD6D5
113 // String format : D1D2D3D4D5D6
115 //-----------------------------------------------------------------------------
120 func (bcd *Tbcd) Encode(str string) []byte {
121 buf := make([]byte, len(str)/2+len(str)%2)
122 for i := 0; i < len(str); i++ {
123 var schar int = bcd.index(str[i])
129 buf[i/2] |= (uint8)(schar) << 4
131 buf[i/2] = 0xf0 | ((uint8)(schar) & 0x0f)
137 func (bcd *Tbcd) Decode(buf []byte) string {
139 for i := 0; i < len(buf); i++ {
141 b = bcd.byte(int(buf[i] & 0x0f))
145 strbytes = append(strbytes, b)
147 b = bcd.byte(int(buf[i] >> 4))
151 strbytes = append(strbytes, b)
153 return string(strbytes)
156 //-----------------------------------------------------------------------------
158 //-----------------------------------------------------------------------------
160 func NewBcd(convTbl string) *Bcd {
162 if len(convTbl) == 16 {
165 b.convtbl = "0123456789?????f"
170 func NewTbcd(convTbl string) *Tbcd {
172 if len(convTbl) == 16 {
175 b.convtbl = "0123456789*#abcf"
180 //-----------------------------------------------------------------------------
182 //-----------------------------------------------------------------------------
187 //-----------------------------------------------------------------------------
189 //-----------------------------------------------------------------------------