whyclj
2021-01-05 64aba412c2b739d67795b14a3cae069d311697f9
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.intelligt.modbus.jlibmodbus.utils;
 
import java.util.Locale;
 
/*
 * Copyright (C) 2016 "Invertor" Factory", JSC
 * [http://www.sbp-invertor.ru]
 *
 * This file is part of JLibModbus.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Authors: Vladislav Y. Kochedykov, software engineer.
 * email: vladislav.kochedykov@gmail.com
 */
public class DataUtils {
 
    static public int fromAscii(char h, char l) {
        try {
            return Integer.parseInt("" + h + l, 16);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return 0;
        }
    }
 
    static public String toAscii(byte b) {
        return toHexString(b);
    }
 
    static public String toAscii(byte[] bytes) {
        return toAscii(bytes, 0, bytes.length);
    }
 
    static public String toAscii(byte[] bytes, int offset, int length) {
        StringBuilder sb = new StringBuilder();
        int tail = offset + length;
 
        for (int i = offset; i < tail; i++) {
            sb.append(toHexString(bytes[i]));
        }
 
        return sb.toString().toUpperCase(Locale.ENGLISH);
    }
 
    static public String toHexString(byte b) {
        return (((b & 0xff) < 0x10) ? "0" : "") + Integer.toString(b & 0xff, 16).toUpperCase(Locale.ENGLISH);
    }
 
    static public byte[] toByteArray(boolean[] bits) {
        byte[] dst = new byte[(int) Math.ceil((double) bits.length / 8)];
        for (int i = 0; i < bits.length; i++) {
            dst[i / 8] |= (byte) ((bits[i] ? 1 : 0) << (i % 8));
        }
        return dst;
    }
 
    static public boolean[] toBitsArray(byte[] bytes, int bitCount) {
        boolean[] dst = new boolean[bitCount];
        for (int i = 0; i < dst.length; i++) {
            dst[i] = (bytes[i / 8] & (1 << (i % 8))) != 0;
        }
        return dst;
    }
 
    static public int[] BeToIntArray(byte[] bytes) {
        int[] dst = new int[bytes.length / 2];
        for (int i = 0, j = 0; i < dst.length; i++, j += 2)
            dst[i] = ((bytes[j] & 0xff) << 8) | (bytes[j + 1] & 0xff);
        return dst;
    }
 
    static public int[] LeToIntArray(byte[] bytes) {
        int[] dst = new int[bytes.length / 2];
        for (int i = 0, j = 0; i < dst.length; i++, j += 2)
            dst[i] = ((bytes[j + 1] & 0xff) << 8) | (bytes[j] & 0xff);
        return dst;
    }
 
    static public byte[] toByteArray(int[] i16) {
        byte[] dst = new byte[i16.length * 2];
        for (int i = 0, j = 0; i < i16.length; i++, j += 2) {
            dst[j] = (byte) ((i16[i] >> 8) & 0xff);
            dst[j + 1] = (byte) (i16[i] & 0xff);
        }
        return dst;
    }
 
    static public byte[] toByteArray(short[] src) {
        byte[] dst = new byte[src.length];
        for (int i = 0; i < src.length; i++)
            dst[i] = (byte) src[i];
        return dst;
    }
 
    public static byte[] toByteArray(short i16) {
        return new byte[]{byteHigh(i16), byteLow(i16)};
    }
 
    public static int toShort(int h, int l) {
        return ((h & 0xff) << 8) | (l & 0xff);
    }
 
    public static byte[] toByteArray(int i32) {
        byte[] regs = new byte[4];
        regs[0] = (byte) (0xff & (i32 >> 8));
        regs[1] = (byte) (0xff & i32);
        regs[2] = (byte) (0xff & (i32 >> 24));
        regs[3] = (byte) (0xff & (i32 >> 16));
        return regs;
    }
 
    public static float toFloat(byte[] bytes) {
        return Float.intBitsToFloat(
                ((bytes[0] & 0xff) << 8) |
                        (bytes[1] & 0xff) |
                        ((bytes[2] & 0xff) << 24) |
                        ((bytes[3] & 0xff) << 16));
    }
 
    public static byte[] toByteArray(float f32) {
        return toByteArray(Float.floatToIntBits(f32));
    }
 
    public static byte byteLow(int b) {
        return (byte) ((short) b & 0xff);
    }
 
    public static byte byteHigh(int b) {
        return (byte) (((short) b >> 8) & 0xff);
    }
 
    public static int wordLow(int b) {
        return b & 0xffff;
    }
 
    public static int wordHigh(int b) {
        return (b >> 16) & 0xffff;
    }
 
    public static int dwordLow(long l) {
        return (int) (l & 0xffffffff);
    }
 
    public static int dwordHigh(long l) {
        return (int) ((l >> 32) & 0xffffffff);
    }
 
    public static int wordFromBytes(int l, int h) {
        return (l & 0xff) | ((h & 0xff) << 8);
    }
 
    public static int dwordFromWords(int l, int h) {
        return (l & 0xffff) | ((h & 0xffff) << 16);
    }
 
    public static long Int64FromDwords(int l, int h) {
        return ((long) l & 0xffffffff) | (((long) h & 0xffffffff) << 32);
    }
}