📄 typestools.java
字号:
package com.sxforever.protocol.cmpp30;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
/**
* <p>Title: sxforever</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: Hengyuan_COM</p>
*
* @author shanying
* @version 1.0
*/
public class TypesTools {
private static final char[] HEX_CODE = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f'};
private static String HexCode[] = {
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"a", "b", "c", "d", "e", "f"
};
private TypesTools() {
}
public static String byteToHexString(byte b) {
int n = b;
if (n < 0)
n = 256 + n;
int d1 = n / 16;
int d2 = n % 16;
return HexCode[d1] + HexCode[d2];
}
public static String byteArrayToHexString(byte b[]) {
return byteArrayToHexString(b, 0, b.length);
}
public static String byteArrayToHexString(byte b[], int offset, int length) {
StringBuffer result = new StringBuffer(length * 2);
for (int i = 0; i < length; i++) {
int n = b[offset + i];
if (n < 0)
n = 256 + n;
result.insert(2 * i, HEX_CODE[(n >> 4)]);
result.insert(2 * i + 1, HEX_CODE[n & 0x0f]);
}
return result.toString();
}
public static void main(String[] args) {
byte[] data = new byte[]{
0x00, 0x01, (byte) 0xf1, (byte) 0xff, (byte) 0xfe, (byte) 0x91};
System.out.println(byteArrayToHexString(data));
}
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[]) {
return b[3] & 0xff | (b[2] & 0xff) << 8 | (b[1] & 0xff) << 16 |
(b[0] & 0xff) << 24;
}
public static short byte2short(byte b[]) {
return (short) (b[1] & 0xff | (b[0] & 0xff) << 8);
}
public static int byte2unsignedShort(byte b[], int offset) {
return b[offset + 1] & 0xff | (b[offset] & 0xff) << 8;
}
public static int unsignedByte2int(byte b) {
return b & 0xff;
}
public static long byte2long(byte b[]) {
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;
}
/**
* n 为待转数据,buf[]为转换后的数据,offset为buf[]中转换的起始点
* 转换后数据从低到高位
*/
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 boolean checkMobile(String sMobile) {
String sF6 = "", sB7 = "", sF2 = "";
if (sMobile == null)
return false;
if (sMobile.length() != 11)
return false;
sF6 = sMobile.substring(0, 7);
sF2 = sMobile.substring(0, 2);
sB7 = sMobile.substring(7);
try {
int iT = Integer.valueOf(sF6).intValue();
iT = Integer.valueOf(sB7).intValue();
if (sF2.equals("13"))
return true;
else
return false;
} catch (Exception ex) {
return false;
}
}
public static String getNowDate10() {
Date nowDate = new Date();
DateFormat DateFmt = new SimpleDateFormat("MMddHHmmss");
String sTimestamp = DateFmt.format(nowDate);
return sTimestamp;
}
public static String getNowDate14() {
Date nowDate = new Date();
DateFormat DateFmt = new SimpleDateFormat("YYMMddHHmmssS");
String sTimestamp = DateFmt.format(nowDate);
return sTimestamp+"08+";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -