public static byte[] str2bytes(int n, String str) {
byte[] temp = new byte[n];
byte[] strByte = str.getBytes(Charset.forName("GBK"));
int length = n >= strByte.length ? strByte.length : n;
System.arraycopy(strByte, 0, temp, 0, length);
return temp;
}
package com.common.util;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class ByteUtil {
private static int INT8_MASK = 0xFF;
private static int INT16_MASK = 0xFFFF;
private static long INT32_MASK = 0xFFFFFFFFL;
public static long[] DECIMALISM = { 1000000000000000000L,
100000000000000000L, 10000000000000000L, 1000000000000000L,
100000000000000L, 10000000000000L, 1000000000000L, 100000000000L,
10000000000L, 1000000000L, 100000000L, 10000000L, 1000000L,
100000L, 10000L, 1000L, 100L, 10L, 1L };
public static int int8(ByteBuffer buf) {
return buf.get();
}
public static int uint8(ByteBuffer buf) {
return buf.get() & INT8_MASK;
}
public static int int16(ByteBuffer buf) {
return buf.getShort();
}
public static int uint16(ByteBuffer buf) {
return buf.getShort() & INT16_MASK;
}
public static long int32(ByteBuffer buf) {
return buf.getInt();
}
public static long uint32(ByteBuffer buf) {
return buf.getInt() & INT32_MASK;
}
public static long int64(ByteBuffer buf) {
return buf.getLong();
}
public static String string(ByteBuffer buf) {
List<Byte> list = new ArrayList<Byte>(10);
byte b = -1;
while ((b = buf.get()) != 0) {
list.add(b);
}
byte[] bs = new byte[list.size()];
for (int i = 0; i < bs.length; i++) {
bs[i] = list.get(i);
}
return new String(bs);
}
public static String string(ByteBuffer buf, int length) {
byte[] bs = new byte[length];
buf.get(bs);
return new String(bs);
}
public static String gbk(ByteBuffer buf, int length) throws Exception{
byte[] bs = new byte[length];
buf.get(bs);
int real = 0;
for (real = 0; real < bs.length && bs[real] != 0; real++);
return new String(bs, 0, real, "GBK");
}
public static String gbk(ByteBuffer buf) throws Exception{
byte[] bs = new byte[1024];
int index = 0;
while (buf.hasRemaining()) {
byte b = buf.get();
if (b == 0) {
break;
}
bs[index++] = b;
}
return new String(bs, 0, index, "GBK");
}
public static byte[] bytes(ByteBuffer buf, int length) {
byte[] bs = new byte[length];
buf.get(bs);
return bs;
}
public static void skip(ByteBuffer buf, int length){
if (buf.remaining() >= length) {
buf.position(buf.position() + length);
}
}
public static String removeSpace(ByteBuffer buf, int lenght) throws Exception{
byte space = 0x20;
byte[] bs = new byte[lenght];
buf.get(bs);
int real = 0;
for (; real < bs.length && bs[real] != space; real++);
return new String(bs, 0, real, "GBK");
}
public static String ascii(ByteBuffer buf, int lenght) {
byte[] bs = new byte[lenght];
buf.get(bs);
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < bs.length; i++) {
buffer.append((char)bs[i]);
}
return buffer.toString();
}
public static long bcd(ByteBuffer buf, int len) {
byte[] bs = new byte[len];
buf.get(bs);
long result = 0;
for (int i = 0; i < len; i++) {
result += DECIMALISM[DECIMALISM.length - len * 2 + i * 2 + 1]
* (bs[i] & 0x0F);
result += DECIMALISM[DECIMALISM.length - len * 2 + i * 2]
* ((bs[i] >> 4) & 0x0F);
}
return result;
}
public static String bcdString(ByteBuffer buf, int len) {
byte[] bs = new byte[len];
buf.get(bs);
String str = "";
for (int i = 0; i < bs.length; i++) {
byte e = bs[i];
str += Integer.toHexString((e >> 4) & 0x0f);
str += Integer.toHexString(e & 0x0f);
}
return str;
}
public static Date date(ByteBuffer buf) {
int year = uint8(buf) + 2000;
int month = uint8(buf);
int date = uint8(buf);
int hour = uint8(buf);
int minute = uint8(buf);
int second = uint8(buf);
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, date, hour, minute, second);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
public static Date bcdDate(ByteBuffer buf) {
int year = (int) (bcd(buf, 1) + 2000);
int month = (int) bcd(buf, 1);
int date = (int) bcd(buf, 1);
int hour = (int) bcd(buf, 1);
int minute = (int) bcd(buf, 1);
int second = (int) bcd(buf, 1);
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, date, hour, minute, second);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
public static Date bcdDateFullYear(ByteBuffer buf) {
int year = (int) bcd(buf, 2);
int month = (int) bcd(buf, 1);
int date = (int) bcd(buf, 1);
int hour = (int) bcd(buf, 1);
int minute = (int) bcd(buf, 1);
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, date, hour, minute);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
public static Date bcdDateNoHMS(ByteBuffer buf) {
int year = (int) (bcd(buf, 1) + 2000);
int month = (int) bcd(buf, 1);
int date = (int) bcd(buf, 1);
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, date);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
public static Date bcdDateFullYearNoMS(ByteBuffer buf) {
int year = (int) bcd(buf, 2);
int month = (int) bcd(buf, 1);
int date = (int) bcd(buf, 1);
int hour = (int) bcd(buf, 1);
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, date, hour, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
public static void putDate(ByteBuffer buf, Date date) {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR) - 2000;
int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
byte[] bs = new byte[] { (byte) year, (byte) month, (byte) day,
(byte) hour, (byte) minute, (byte) second };
buf.put(bs);
}
public static void putString(ByteBuffer buf, String str)
throws Exception {
byte[] bs = str.getBytes("GBK");
buf.put(bs);
buf.put((byte) 0);
}
public static void putGbkString(ByteBuffer buf, String str) throws Exception{
byte[] bs = null;
bs = str.getBytes("GBK");
buf.put(bs);
}
public static void putBcd(ByteBuffer buf, long l, int len) {
for (int i = DECIMALISM.length - len * 2; i < DECIMALISM.length; i += 2) {
byte b = 0;
b = (byte) ((l / DECIMALISM[i] % 10) << 4);
b |= (l / DECIMALISM[i + 1] % 10);
buf.put(b);
}
}
public static void putBcdDate(ByteBuffer buf, Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
putBcd(buf, c.get(Calendar.YEAR) - 2000, 1);
putBcd(buf, c.get(Calendar.MONTH) + 1, 1);
putBcd(buf, c.get(Calendar.DAY_OF_MONTH), 1);
putBcd(buf, c.get(Calendar.HOUR_OF_DAY), 1);
putBcd(buf, c.get(Calendar.MINUTE), 1);
putBcd(buf, c.get(Calendar.SECOND), 1);
}
public static String hex(byte[] buf){
StringBuilder sb = new StringBuilder();
String table = "0123456789ABCDEF";
for (int i = 0; i < buf.length; i++) {
sb.append(table.charAt(buf[i] >> 4 & 0x0f));
sb.append(table.charAt(buf[i] & 0x0f));
}
return sb.toString();
}
public static String bytes2Hex(byte[] buf) {
if (null == buf) {
return "";
}
StringBuilder s = new StringBuilder();
for (int i = 0; i < buf.length; ++i) {
s.append(String.format("%02X ", buf[i]));
}
s.deleteCharAt(s.length()-1);
return s.toString().replaceAll(" ", "");
}
public static byte[] hex2Byte(String msg) {
if (msg == null || msg.isEmpty()){
return null;
}
String data = msg.toUpperCase();
if ((data.length() % 2) != 0){
data = msg + "0";
}
int len = data.length() / 2;
ByteBuffer buf = ByteBuffer.allocate(len);
for (int i = 0; i < data.length(); i += 2){
int cl = hexChar2Int(data.codePointAt(i));
int cr = hexChar2Int(data.codePointAt(i + 1));
byte b = (byte) ((cl << 4 & 0xF0) | (cr & 0x0F));
buf.put(b);
}
return buf.array();
}
public static int hexChar2Int(int c){
if (c >= 0x30 && c <= 0x39){
return c - 0x30;
}
if (c >= 0x41 && c <= 0x46){
return c - 0x37;
}
return 0;
}
}