?? typeconvert.java
字號:
package com.aceway.vas.commons.tcp.common;
public class TypeConvert {
public TypeConvert() {
}
public static int byte2int(byte b[], int offset) {
return b[offset + 3] & 0xff | (b[offset + 2] & 0xff) << 8
| (b[offset + 1] & 0xff) << 16 | (b[offset] & 0xff) << 24;
}
public static int byte2int(byte b[]) {
int len = b.length;
byte[] bb = null;
if (len < 4) {
bb = new byte[4];
System.arraycopy(b, 0, bb, 4 - len, len);
b = bb;
}
return b[3] & 0xff | (b[2] & 0xff) << 8 | (b[1] & 0xff) << 16
| (b[0] & 0xff) << 24;// default is int
}
public static long byte2long(byte b[]) {
int len = b.length;
byte[] bb = null;
if (len < 8) {
bb = new byte[8];
System.arraycopy(b, 0, bb, 8 - len, len);
b = bb;
}
return (long) b[7] & (long) 255 | ((long) b[6] & (long) 255) << 8
| ((long) b[5] & (long) 255) << 16
| ((long) b[4] & (long) 255) << 24
| ((long) b[3] & (long) 255) << 32
| ((long) b[2] & (long) 255) << 40
| ((long) b[1] & (long) 255) << 48 | (long) b[0] << 56;
}
public static long byte2long(byte b[], int offset) {
return (long) b[offset + 7] & (long) 255
| ((long) b[offset + 6] & (long) 255) << 8
| ((long) b[offset + 5] & (long) 255) << 16
| ((long) b[offset + 4] & (long) 255) << 24
| ((long) b[offset + 3] & (long) 255) << 32
| ((long) b[offset + 2] & (long) 255) << 40
| ((long) b[offset + 1] & (long) 255) << 48
| (long) b[offset] << 56;
}
public static byte[] int2byte(int n) {
byte b[] = new byte[4];
b[0] = (byte) (n >> 24);
b[1] = (byte) (n >> 16);
b[2] = (byte) (n >> 8);
b[3] = (byte) n;
return b;
}
public static void int2byte(int n, byte buf[], int offset) {
buf[offset] = (byte) (n >> 24);
buf[offset + 1] = (byte) (n >> 16);
buf[offset + 2] = (byte) (n >> 8);
buf[offset + 3] = (byte) n;
}
public static byte[] short2byte(int n) {
byte b[] = new byte[2];
b[0] = (byte) (n >> 8);
b[1] = (byte) n;
return b;
}
public static void short2byte(int n, byte buf[], int offset) {
buf[offset] = (byte) (n >> 8);
buf[offset + 1] = (byte) n;
}
public static byte[] long2byte(long n) {
byte b[] = new byte[8];
b[0] = (byte) (int) (n >> 56);
b[1] = (byte) (int) (n >> 48);
b[2] = (byte) (int) (n >> 40);
b[3] = (byte) (int) (n >> 32);
b[4] = (byte) (int) (n >> 24);
b[5] = (byte) (int) (n >> 16);
b[6] = (byte) (int) (n >> 8);
b[7] = (byte) (int) n;
return b;
}
public static void long2byte(long n, byte buf[], int offset) {
buf[offset] = (byte) (int) (n >> 56);
buf[offset + 1] = (byte) (int) (n >> 48);
buf[offset + 2] = (byte) (int) (n >> 40);
buf[offset + 3] = (byte) (int) (n >> 32);
buf[offset + 4] = (byte) (int) (n >> 24);
buf[offset + 5] = (byte) (int) (n >> 16);
buf[offset + 6] = (byte) (int) (n >> 8);
buf[offset + 7] = (byte) (int) n;
}
public static byte[] toBCD(int num) {
byte[] data = new byte[10];
int next = num;
int count = 0;
for (int i = 0; next != 0; i++) {
int index = i / 2;
byte yu = (byte) (next % 10);
if (i % 2 == 0)
data[index] = yu;
else {
int tmp = yu << 4;
tmp = data[index] + tmp;
data[index] = (byte) (tmp);
}
count++;
next = next / 10;
}
int size = (count % 2 == 0 && count != 0) ? count / 2 : count / 2 + 1;
byte[] result = new byte[size];
System.arraycopy(data, 0, result, 0, size);
return result;
}
public static int BCDtoINT(byte[] num) {
if (num == null)
return 0;
int len = num.length;
byte[] bcd = new byte[len];
System.arraycopy(num, 0, bcd, 0, len);
int result = 0;
int exp = 1;
for (int i = 0; i < len; i++) {
int low = bcd[i] & 0x0f;
result += low * exp;
exp *= 10;
// bcd[i]>>>4 is wrong,because byte is extended to int before
// operation
int hi = (bcd[i] & 0xf0) >>> 4;
result += hi * exp;
exp *= 10;
}
return result;
}
public static void main(String[] arg) {
for (int i = 0; i < 123456789; i++) {
byte[] test = toBCD(i);
int result = BCDtoINT(test);
System.out.println(result);
try {
Thread.sleep(10);
} catch (Exception e) {
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -