tools.java
来自「采用JAVA开发」· Java 代码 · 共 198 行
JAVA
198 行
package com.gctech.util;
/**
* <p>Title: 工具类</p>
* <p>Description: 工具类</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: GCTech</p>
* @author 王红宝
* @version $Id: Tools.java,v 1.1.1.1 2004/04/21 09:30:42 wanghb Exp $
*/
public class Tools {
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 Tools() {
}
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;
}
/**
* 将四个字节转化为整数
*
* @param mybytes
* @return
* add by liyi 2004-7-27
*/
public static int Bytes4ToInt(byte mybytes[]) {
int tmp = (0xff & mybytes[0]) << 24 | (0xff & mybytes[1]) << 16 | (0xff & mybytes[2]) << 8 | 0xff & mybytes[3];
return tmp;
}
/**
* 将整数转化为四个字节的数组
*
* @param mybytes
* @return
* add by liyi 2004-7-27
*/
public static byte[] IntToBytes4(int i) {
byte mybytes[] = new byte[4];
mybytes[3] = (byte) (0xff & i);
mybytes[2] = (byte) ((0xff00 & i) >> 8);
mybytes[1] = (byte) ((0xff0000 & i) >> 16);
mybytes[0] = (byte) ((0xff000000 & i) >> 24);
return mybytes;
}
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 void BytesCopy(byte source[], byte dest[], int sourcebegin, int sourceend, int destbegin) {
int j = 0;
for (int i = sourcebegin; i <= sourceend; i++) {
dest[destbegin + j] = source[i];
j++;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?