首页 > 代码库 > java格式处理工具类
java格式处理工具类
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* java格式处理工具类
*
* @author lay at 2014年8月7日09:12:20
*
* @version 1.0
*/
public final class EncodingUtil {
/**
* 将十进制整数转为byte数组(2字节) 仅限于2字节以内的十进制整数
*
* @param value
* 要转换的值
* @param flag
* 顺序选择,true高位在前,低位在后
* @return
*/
public static byte[] int2Bytes2(int value, boolean flag) {
if (flag) {
byte c[] = { (byte) ((value >>> 8) & 0xff), (byte) (value & 0xff) };
return c;
} else {
byte c[] = { (byte) (value & 0xff), (byte) ((value >>> 8) & 0xff) };
return c;
}
}
/**
* 将十进制整数转为byte数组(3字节) 仅限于3字节以内的十进制整数
*
* @param value
* 要转换的值
* @param flag
* 顺序选择,true高位在前,低位在后
* @return
*/
public static byte[] int2Bytes3(int value, boolean flag) {
if (flag) {
byte[] b = { (byte) ((value >>> 16) & 0xff), (byte) (value >>> 8),
(byte) (value & 0xff) };
return b;
} else {
byte[] b = { (byte) (value & 0xff), (byte) (value >>> 8),
(byte) ((value >>> 16) & 0xff) };
return b;
}
}
/**
* 将十进制整数转为byte数组(四字节)
*
* @param value
* 要转换的值
* @param flag
* 顺序选择,true高位在前,低位在后
* @return
*/
public static byte[] int2Bytes4(int value, boolean flag) {
byte[] src = new byte[4];
if (flag) {
src[0] = (byte) ((value >>> 24) & 0xFF);
src[1] = (byte) ((value >>> 16) & 0xFF);
src[2] = (byte) ((value >>> 8) & 0xFF);
src[3] = (byte) (value & 0xFF);
} else {
src[3] = (byte) ((value >>> 24) & 0xFF);
src[2] = (byte) ((value >>> 16) & 0xFF);
src[1] = (byte) ((value >>> 8) & 0xFF);
src[0] = (byte) (value & 0xFF);
}
return src;
}
/**
* 将byte数组转为十进制整数(四字节) 数组长度大于4时,按长度为4处理
*
* @param src
* 高位在前的长度为4的byte数组
* @return
*/
public static int byte2Int(byte[] src) {
int value;
switch (src.length) {
case 0:
value = 0;
break;
case 1:
value = (int) (src[0] & 0xFF);
break;
case 2:
value = (int) ((src[1] & 0xFF) | ((src[0] & 0xFF) << 8));
break;
case 3:
value = (int) ((src[2] & 0xFF) | ((src[1] & 0xFF) << 8) | ((src[0] & 0xFF) << 16));
break;
case 4:
value = (int) ((src[3] & 0xFF) | ((src[2] & 0xFF) << 8)
| ((src[1] & 0xFF) << 16) | ((src[0] & 0xFF) << 24));
break;
default:
value = (int) ((src[3] & 0xFF) | ((src[2] & 0xFF) << 8)
| ((src[1] & 0xFF) << 16) | ((src[0] & 0xFF) << 24));
break;
}
return value;
}
/**
* 将byte转为十六进制字符串,不会补0
*
* @param src
* 高位在前的长度为4的byte数组
* @return
*/
public static String byte2Hex(byte b) {
return Integer.toHexString(0xFF & b).toUpperCase();
}
/**
* 十六进制字符串前面补0
*
* @param hex
* 要补0的字符串
* @param len
* 补0后的长度
* @return
*/
public static String addZeroAtStart(String hex, int len) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < len - hex.length(); i++) {
sb.append("0");
}
sb.append(hex);
return sb.toString().toUpperCase();
}
/**
* 十六进制字符串后面补0
*
* @param hex
* 要补0的字符串
* @param len
* 补0后的长度
* @return
*/
public static String addZeroAtEnd(String hex, int len) {
StringBuffer sb = new StringBuffer();
sb.append(hex);
for (int i = 0; i < len - hex.length(); i++) {
sb.append("0");
}
return sb.toString().toUpperCase();
}
/**
* 1字节16进制字符串转byte
*
* @param value
* @return
*/
public static byte hex2Byte(String value) {
return (byte) (Integer.parseInt(value, 16) & 0xff);
}
/**
* 将16进制string当做一个整数转为byte数组
*
* @param crc
* @return
*/
public static byte[] hexs2Bytes2(String hex) {
BigInteger bi = new BigInteger(hex, 16);
return bi.toByteArray();
}
/**
* 将byte数组当成一个整数转换为16进制string
*
* @param bArray
* @return
*/
public static String bytes2Hexs2(byte[] bArray) {
BigInteger bi = new BigInteger(bArray);
return addZeroAtStart(bi.toString(16), bArray.length * 2);
}
/**
* 将byte数组当成字符数组转换为16进制string
*
* @param bArray
* @return
*/
public static String bytes2Hexs(byte[] bArray) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bArray.length; i++) {
sb.append(addZeroAtStart(byte2Hex(bArray[i]), 2));
}
return sb.toString();
}
/**
* 把可序列化对象转换成字节数组
*
* @param s
* @return
* @throws IOException
*/
public static final byte[] object2Bytes(Serializable s) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream ot = new ObjectOutputStream(out);
ot.writeObject(s);
ot.flush();
ot.close();
return out.toByteArray();
}
/**
* 将时间转换为4字节byte数组
*
* @param time
* @return
* @throws ParseException
*/
public static byte[] time2Bytes(String time) throws ParseException {
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
Calendar c = Calendar.getInstance();
c.setTime(date);
int as = (c.get(Calendar.HOUR_OF_DAY) * 60 + c.get(Calendar.MINUTE))
* 60 + c.get(Calendar.SECOND);
String hex = addZeroAtStart(Integer.toHexString(as), 8);
byte b[] = new byte[4];
for (int i = 0; i < 4; i++) {
b[i] = (byte) hex2Byte(hex.substring(i * 2, i * 2 + 2));
}
return b;
}
/**
* 日期转长度为3的byte数组对应年月日
*
* @param d
* @return
*/
public static byte[] date2Bytes(Date d) {
Calendar c = Calendar.getInstance();
c.setTime(d);
int year = c.get(Calendar.YEAR) % 100;
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
byte[] b = new byte[3];
b[0] = (byte) (year & 0xff);
b[1] = (byte) (month & 0xff);
b[2] = (byte) (day & 0xff);
return b;
}
/**
* 处理Get请求产生的乱码 处理失败返回""
*
* @param temp
* 乱码
* @return 正确编码的字符串
*/
public static String string2GBK(String temp) {
try {
byte s[] = temp.getBytes("iso-8859-1");
return new String(s, "GBK");
} catch (Exception e) {
return "";
}
}
/**
* byte数组转以BCD码格式转为字符串
*
* @param bytes
* BCD码
* @return 10进制串
*/
public static String bytes2BCD(byte[] bytes) {
StringBuffer temp = new StringBuffer(bytes.length * 2);
for (int i = 0; i < bytes.length; i++) {
temp.append((byte) ((bytes[i] & 0xf0) >>> 4));
temp.append((byte) (bytes[i] & 0x0f));
}
return temp.toString();
}
/**
* BCD码格式字符串转为byte数组
*
* @param bytes
* BCD码
* @return 10进制串
*/
public static byte[] BCDString2Bytes(String str) {
int len = str.length();
if (len % 2 != 0) {
str = "0" + str;
}
byte[] b = new byte[str.length() / 2];
for (int i = 0; i < b.length; i++) {
b[i] = (byte) (((((str.charAt(i * 2) - ‘0‘) << 4) & 0xf0) | ((str
.charAt(i * 2 + 1) - ‘0‘) & 0x0f)) & 0xff);
}
return b;
}
/**
* BCD码格式字符串转为byte数组
*
* @param bytes
* BCD码
* @return 10进制串
*/
public static String bytes2BCDString(byte[] b) {
return bytes2Hexs(b);
}
/**
* 10进制串转为BCD码
*
* @param asc
* 10进制串
* @return BCD码
*/
public static byte[] des2BCD(String des) {
int len = des.length();
if (len % 2 != 0) {
des = "0" + des;
}
byte[] b = new byte[des.length() / 2];
for (int i = 0; i < b.length; i++) {
b[i] = (byte) ((((des.charAt(i * 2) - ‘0‘) << 4) & 0xf0) | ((des
.charAt(i * 2 + 1) - ‘0‘) & 0x0f));
}
return b;
}
/**
* 10进制串转为BCD码
*
* @param bcd
* BCD码byte数组
* @return BCD码
*/
public static int BCD2Des(byte[] bcd) {
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < bcd.length; i++) {
sb.append((char) (((bcd[i] >>> 4) & 0x0f) + ‘0‘));
sb.append((char) ((bcd[i] & 0x0f) + ‘0‘));
}
return Integer.parseInt(sb.toString());
}
/**
* 将16进制字符串每两个字当做一个字节转换为byte数组
*
* @param hex
* @return
*/
public static byte[] hexs2Bytes(String hex) {
int len = hex.length();
if (len % 2 != 0) {
hex = "0" + hex;
}
byte[] result = new byte[len / 2];
for (int i = 0; i < result.length; i++) {
result[i] = hex2Byte(hex.substring(i * 2, i * 2 + 2));
}
return result;
}
/**
* char数组转16进制字符串
*
* @param c
* @return
*/
public static final String chars2Hexs(char[] c) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < c.length; i++) {
sb.append(addZeroAtStart(Integer.toHexString((int) c[i])
.toUpperCase(), 2));
}
return sb.toString();
}
/**
* 16进制字符串转char数组
*
* @param c
* @return
*/
public static final char[] Hexs2Chars(String str) {
int len = str.length();
if (len % 2 != 0) {
str = "0" + str;
}
char[] c = new char[str.length() / 2];
for (int i = 0; i < str.length() / 2; i++) {
c[i] = (char) hex2Des(str.substring(i * 2, i * 2 + 2));
}
return c;
}
/**
* 把字节数组转换为对象
*
* @param bytes
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
public static final Object bytes2Object(byte[] bytes) throws IOException,
ClassNotFoundException {
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
ObjectInputStream oi = new ObjectInputStream(in);
Object o = oi.readObject();
oi.close();
return o;
}
/**
* 10进制转16进制
*
* @param num
* @return
*/
public static final String des2Hexs(int num) {
return Integer.toHexString(num).toUpperCase();
}
/**
* 16进制转10进制
*
* @param s
* @return
*/
public static int hex2Des(String s) {
return Integer.parseInt(s, 16);
}
/**
* 16进制单字节转char
*
* @param hex
* @return
*/
public static char hex2Char(String hex) {
char c = (char) Integer.parseInt(hex, 16);
return c;
}
/**
* 16进制字符以字节为单位进行分割
*
* @param hex
* 16进制字符串,必须为整数个字节
* @return
*/
public static String[] hexs2hexs(String hex) {
String[] strs = new String[hex.length() / 2];
for (int i = 0; i < hex.length() / 2; i++) {
strs[i] = hex.substring(0 * 2, 0 * 2 + 1);
}
return strs;
}
/**
* 将字符串转为16进制字符串
*
* @param asc
* @return
*/
public static String string2Hexs(String strs) {
String str = String.valueOf(strs);
byte[] b = str.getBytes();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
sb.append(addZeroAtStart(byte2Hex(b[i]), 2));
}
return sb.toString();
}
}