#
luxiaotao1123
2020-08-04 6abc20e29568c129f4ca71eccec9310534a8c779
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.zy.common.HslCommunication.Core.Transfer;
 
import com.zy.common.HslCommunication.BasicFramework.SoftBasic;
import com.zy.common.HslCommunication.Utilities;
 
/**
 * 以字节为单位的变换操作
 */
public class ReverseWordTransform extends ByteTransformBase
{
    /**
     * 实例化一个默认的对象
     */
    public  ReverseWordTransform(){
        this.setDataFormat(DataFormat.ABCD);
    }
 
    /**
     * 按照字节错位的方法
     * @param buffer 实际的字节数据
     * @param index 起始字节位置
     * @param length 数据长度
     * @return
     */
    private byte[] ReverseBytesByWord( byte[] buffer, int index, int length )
    {
        byte[] tmp = new byte[length];
 
        for (int i = 0; i < length; i++)
        {
            tmp[i] = buffer[index + i];
        }
 
        for (int i = 0; i < length / 2; i++)
        {
            byte b = tmp[i * 2 + 0];
            tmp[i * 2 + 0] = tmp[i * 2 + 1];
            tmp[i * 2 + 1] = b;
        }
 
        return tmp;
    }
 
    private byte[] ReverseBytesByWord( byte[] buffer )
    {
        return ReverseBytesByWord( buffer, 0, buffer.length );
    }
 
 
    /**
     * 字符串数据是否按照字来反转
     */
    public boolean IsStringReverse =false;
 
 
 
 
    /**
     * 从缓存中提取short结果
     * @param buffer 缓存数据
     * @param index 索引位置
     * @return short对象
     */
    @Override
    public short TransInt16( byte[] buffer, int index ) {
        return Utilities.getShort(ReverseBytesByWord(buffer, index, 2), 0);
    }
 
 
 
    /**
     * 从缓存中提取string结果,使用指定的编码
     * @param buffer 缓存对象
     * @param index 索引位置
     * @param length byte数组长度
     * @param encoding 字符串的编码
     * @return string对象
     */
    @Override
    public String TransString( byte[] buffer, int index, int length, String encoding ) {
        byte[] tmp = TransByte(buffer, index, length);
 
        if (IsStringReverse) {
            return Utilities.getString(ReverseBytesByWord(tmp), "ASCII");
        } else {
            return Utilities.getString(tmp, "ASCII");
        }
    }
 
 
 
    /**
     * bool数组变量转化缓存数据
     * @param values 等待转化的数组
     * @return buffer数据
     */
    @Override
    public byte[] TransByte( boolean[] values ) {
        return SoftBasic.BoolArrayToByte(values);
    }
 
 
 
    /**
     * short数组变量转化缓存数据
     * @param values 等待转化的数组
     * @return buffer数据
     */
    @Override
    public byte[] TransByte( short[] values ) {
        if (values == null) return null;
 
        byte[] buffer = new byte[values.length * 2];
        for (int i = 0; i < values.length; i++) {
            byte[] tmp = Utilities.getBytes(values[i]);
            System.arraycopy(tmp, 0, buffer, 2 * i, tmp.length);
        }
 
        return ReverseBytesByWord(buffer);
    }
 
 
 
    /**
     * 使用指定的编码字符串转化缓存数据
     * @param value 等待转化的数据
     * @param encoding 字符串的编码方式
     * @return buffer数据
     */
    @Override
    public byte[] TransByte( String value, String encoding ) {
        if (value == null) return null;
        byte[] buffer = Utilities.getBytes(value, encoding);
        buffer = SoftBasic.ArrayExpandToLengthEven(buffer);
        if (IsStringReverse) {
            return ReverseBytesByWord(buffer);
        } else {
            return buffer;
        }
    }
 
}