package com.base;
|
|
import java.nio.ByteBuffer;
|
import java.nio.ByteOrder;
|
|
/**
|
* Êý¾Ýת»»
|
* @author LiJun
|
*
|
*/
|
public class ComBase {
|
|
|
public static byte changeIntToByte(int data)
|
{
|
return (byte)(data & 0xFF);
|
}
|
//------------------------------------------------------------------------------
|
public static short changeIntToShort(int data)
|
{
|
return (short)(data & 0xFFFF);
|
}
|
//------------------------------------------------------------------------------
|
public static byte changeShortToByte(short data)
|
{
|
return (byte)(data & 0xFF);
|
}
|
//------------------------------------------------------------------------------
|
public static int changeByteToInt(byte data)
|
{
|
int tmp = data;
|
return (tmp & 0xFF);
|
}
|
//------------------------------------------------------------------------------
|
public static int changeShortToInt(short data)
|
{
|
int tmp = data;
|
return (tmp & 0xFFFF);
|
}
|
//------------------------------------------------------------------------------
|
public static double changeShortToDouble(short data)
|
{
|
int tmp = data & 0xFFFF;
|
return (double)(tmp);
|
}
|
|
public static int ByteToHex(byte temp1 ,byte temp2) {
|
int Rtemp;
|
Rtemp = ((temp1 &0xff)<<8) | (temp2 & 0xff);
|
return (short)Rtemp;
|
}
|
|
|
public static int ByteToHexxx(byte temp1 ,byte temp2) {
|
int Rtemp;
|
Rtemp = ((temp1 &0xff)<<8) | (temp2 & 0xff);
|
return (int)Rtemp;
|
}
|
|
public static int ByteToInt(byte temp1 ,byte temp2,byte temp3 ,byte temp4){
|
int Rtemp;
|
int Rtemp1,Rtemp2;
|
Rtemp = 0;
|
Rtemp1 = ByteToHexxx( temp3 ,temp4);
|
Rtemp2 = ByteToHexxx( temp1 ,temp2);
|
Rtemp= (int)(Rtemp1<<16) +(int)(Rtemp2);
|
return Rtemp;
|
}
|
|
public static long ByteToLong(byte temp1 ,byte temp2,byte temp3 ,byte temp4){
|
long Rtemp;
|
long Rtemp1,Rtemp2;
|
Rtemp = 0;
|
Rtemp1 = ByteToHexxx( temp3 ,temp4);
|
Rtemp2 = ByteToHexxx( temp1 ,temp2);
|
Rtemp= (int)(Rtemp1<<16) +(int)(Rtemp2);
|
return Rtemp;
|
}
|
|
|
public static byte[] IntToByte(int num){
|
byte[]bytes=new byte[4];
|
bytes[0]=(byte) ((num>>8)&0xff);
|
bytes[1]=(byte) ((num)&0xff);
|
bytes[2]=(byte) ((num>>24)&0xff);
|
bytes[3]=(byte) (num>>16&0xff);
|
return bytes;
|
}
|
|
public static byte[] LongToByte(long num){
|
byte[]bytes=new byte[4];
|
bytes[0]=(byte) ((num>>8)&0xff);
|
bytes[1]=(byte) ((num)&0xff);
|
bytes[2]=(byte) ((num>>24)&0xff);
|
bytes[3]=(byte) (num>>16&0xff);
|
return bytes;
|
}
|
|
public static int my_power_2(int N){
|
StringBuffer v = new StringBuffer("");
|
long num[] = new long[2];
|
num[1] = 1;
|
if(N > 62){
|
num[0] = 1;
|
num[0] = num[0]<<(N - 62);
|
num[1] = num[1]<<62;
|
String s = String.valueOf(num[1]);
|
int size = 30,i = 0,j = 0;
|
long n[] = new long[size + 1];
|
//System.out.println(num[0]+" "+s);
|
for(i = s.length() - 1;i >= 0;-- i){
|
n[j ++] = (long) (num[0] * (s.charAt(i) - '0'));
|
//System.out.println(n[j - 1]);
|
}
|
for(i = 0;i < size;++ i){
|
while(n[i] > 9){
|
n[i + 1] += n[i] / 10;
|
n[i] %= 10;
|
}
|
}
|
boolean bl = false;
|
for(i = size;i >= 0;-- i){
|
if(n[i] != 0 || bl){
|
v.append(n[i]);
|
bl = true;
|
}
|
}
|
}else{
|
num[1] = num[1] << N;
|
v.append(String.valueOf(num[1]));
|
}
|
return Integer.parseInt(v.toString());
|
}
|
|
/**
|
* »ñÈ¡Êý¾ÝÍ·µÄ³¤¶È
|
* @param b
|
* @return
|
*/
|
public static int createPackHeadCount(byte[] b) {
|
ByteBuffer bf = ByteBuffer.allocate(b.length);
|
bf.order(ByteOrder.LITTLE_ENDIAN);
|
bf.put(b);
|
bf.flip();
|
return ComBase.changeShortToInt(bf.getShort());
|
}
|
|
|
/**
|
* ·µ»Ø¼ÆËãºóµÄУÑéÊý×é
|
* @param data
|
* @return
|
*/
|
public static byte[] calCheckSum(byte[] data) {
|
if(data != null && data.length%2 == 0) {
|
//·Ç¿Õ,²¢ÇÒ×Ö½ÚΪżÊý×Ö½Ú
|
ByteBuffer bf = ByteBuffer.allocate(data.length+2);
|
bf.order(ByteOrder.LITTLE_ENDIAN);
|
bf.put(data);
|
short sum = 0;
|
bf.position(0);
|
for(int i = 0;i<data.length/2;i++) {
|
sum += bf.getShort()&0xFFFF;
|
}
|
bf.putShort((short) ((~sum)&0xFFFF));
|
data = bf.array();
|
}
|
return data;
|
}
|
|
|
/**
|
* ¼ì²éÊý¾ÝУÑéºÍ
|
* @param data
|
* @return
|
*/
|
public static boolean checkPackageSum(byte[] data) {
|
boolean flag = false;
|
if(data != null && data.length%2==0) {
|
ByteBuffer bf = ByteBuffer.allocate(data.length);
|
bf.order(ByteOrder.LITTLE_ENDIAN);
|
bf.put(data);
|
bf.flip();
|
short sum = 0 ;
|
for(int i = 0;i<bf.limit()/2;i++) {
|
sum += bf.getShort()&0xFFFF;
|
}
|
if((sum&0xFFFF) == 0xFFFF) {
|
flag = true;
|
}
|
}
|
return flag;
|
}
|
|
/**
|
* ¼ì²éÊý¾ÝУÑéºÍ
|
* @param buffer
|
* @return
|
*/
|
public static boolean checkByteBufferSum(ByteBuffer buffer) {
|
boolean flag = false;
|
if(buffer != null && buffer.limit()%2==0) {
|
ByteBuffer bf = buffer;
|
bf.order(ByteOrder.LITTLE_ENDIAN);
|
bf.position(0);
|
short sum = 0 ;
|
for(int i = 0;i<bf.limit()/2;i++) {
|
sum += bf.getShort()&0xFFFF;
|
}
|
if((sum&0xFFFF) == 0xFFFF) {
|
flag = true;
|
}
|
}
|
return flag;
|
}
|
|
}
|