package com.softkey;
|
|
import java.nio.ByteBuffer;
|
import java.nio.ByteOrder;
|
|
public class Covert
|
{
|
private static int OVER_STRING_LEN= - 8009; //×Ö·û´®³¤¶ÈÒç³ö
|
public static int LastErr;
|
public static void var2Buf(byte[] Buf, int pos, boolean InData, int typeSize)
|
{
|
if(InData)
|
{
|
Buf[pos]=1;
|
}
|
else
|
{
|
Buf[pos]=0;
|
}
|
LastErr=0;
|
}
|
public static void var2Buf(byte[] Buf, int pos, char InData, int typeSize)
|
{
|
Buf[pos]=(byte) InData;
|
LastErr=0;
|
}
|
public static void var2Buf(byte[] Buf, int pos, byte InData, int typeSize)
|
{
|
Buf[pos]=InData;
|
LastErr=0;
|
}
|
public static void var2Buf(byte[] Buf, int pos, short InData, int typeSize)
|
{
|
ByteBuffer buffer=ByteBuffer.wrap(Buf);
|
buffer.order(ByteOrder.LITTLE_ENDIAN);
|
buffer.putShort(pos, InData);
|
LastErr=0;
|
}
|
|
public static void var2Buf(byte[] Buf, int pos, int InData, int typeSize)
|
{
|
ByteBuffer buffer=ByteBuffer.wrap(Buf);
|
buffer.order(ByteOrder.LITTLE_ENDIAN);
|
buffer.putInt(pos, InData);
|
LastErr=0;
|
}
|
|
public static void var2Buf(byte[] Buf, int pos, float InData, int typeSize)
|
{
|
ByteBuffer buffer=ByteBuffer.wrap(Buf);
|
buffer.order(ByteOrder.LITTLE_ENDIAN);
|
buffer.putFloat(pos, InData);
|
LastErr=0;
|
}
|
public static void var2Buf(byte[] Buf, int pos, double InData, int typeSize)
|
{
|
ByteBuffer buffer=ByteBuffer.wrap(Buf);
|
buffer.order(ByteOrder.LITTLE_ENDIAN);
|
buffer.putDouble(pos, InData);
|
LastErr=0;
|
}
|
public static void var2Buf(byte[] Buf, int pos, String InData, int typeSize)
|
{
|
byte[] TmpBuf =InData.getBytes();
|
System.arraycopy(TmpBuf, 0, Buf, pos, TmpBuf.length);
|
if(TmpBuf.length>typeSize)
|
{
|
LastErr=OVER_STRING_LEN;
|
}
|
else
|
{
|
LastErr=0;
|
}
|
}
|
public static void varArray2Buf(byte[] Buf, int pos, String[] InData, int arrSize, int typeSize)
|
{
|
for (int n = 0; n < arrSize; n++)
|
{
|
var2Buf(Buf, pos, InData[n], typeSize);
|
if(LastErr!=0)return ;
|
pos = pos + typeSize;
|
}
|
}
|
public static void varArray2Buf(byte[] Buf, int pos, boolean[] InData, int arrSize, int typeSize)
|
{
|
for (int n = 0; n < arrSize; n++)
|
{
|
var2Buf(Buf, pos, InData[n], typeSize);
|
pos = pos + typeSize;
|
}
|
}
|
public static void varArray2Buf(byte[] Buf, int pos, char[] InData,int arrSize,int typeSize)
|
{
|
for (int n = 0; n < arrSize; n++)
|
{
|
var2Buf(Buf, pos, InData[n], typeSize);
|
pos = pos + typeSize;
|
}
|
}
|
|
public static void varArray2Buf(byte[] Buf, int pos, byte[] InData, int arrSize, int typeSize)
|
{
|
for (int n = 0; n < arrSize; n++)
|
{
|
var2Buf(Buf, pos, InData[n], typeSize);
|
pos = pos + typeSize;
|
}
|
}
|
public static void varArray2Buf(byte[] Buf, int pos, short [] InData, int arrSize, int typeSize)
|
{
|
for (int n = 0; n < arrSize; n++)
|
{
|
var2Buf(Buf, pos, InData[n], typeSize);
|
pos = pos + typeSize;
|
}
|
}
|
|
public static void varArray2Buf(byte[] Buf, int pos, int [] InData, int arrSize, int typeSize)
|
{
|
for (int n = 0; n < arrSize; n++)
|
{
|
var2Buf(Buf, pos, InData[n], typeSize);
|
pos = pos + typeSize;
|
}
|
}
|
|
public static void varArray2Buf(byte[] Buf, int pos, float[] InData, int arrSize, int typeSize)
|
{
|
for (int n = 0; n < arrSize; n++)
|
{
|
var2Buf(Buf, pos, InData[n], typeSize);
|
pos = pos + typeSize;
|
}
|
}
|
public static void varArray2Buf(byte[] Buf, int pos, double[] InData, int arrSize, int typeSize)
|
{
|
for (int n = 0; n < arrSize; n++)
|
{
|
var2Buf(Buf, pos, InData[n], typeSize);
|
pos = pos + typeSize;
|
}
|
}
|
|
public static boolean buf2Bool(byte[] Buf, int pos)
|
{
|
LastErr=0;
|
if(Buf[pos]==1)return true;
|
return false;
|
}
|
public static char buf2Char(byte[] Buf, int pos)
|
{
|
LastErr=0;
|
return (char)Buf[pos];
|
}
|
public static byte buf2Byte(byte[] Buf, int pos)
|
{
|
LastErr=0;
|
return Buf[pos];
|
}
|
public static short buf2Short( byte[] Buf, int pos)
|
{
|
LastErr=0;
|
ByteBuffer buffer = ByteBuffer.wrap(Buf);
|
buffer.order(ByteOrder.LITTLE_ENDIAN);
|
return buffer.getShort(pos);
|
}
|
public static int buf2Int(byte[] Buf, int pos)
|
{
|
LastErr=0;
|
ByteBuffer buffer = ByteBuffer.wrap(Buf);
|
buffer.order(ByteOrder.LITTLE_ENDIAN);
|
return buffer.getInt(pos);
|
}
|
public static float buf2Float(byte[] Buf, int pos)
|
{
|
LastErr=0;
|
ByteBuffer buffer = ByteBuffer.wrap(Buf);
|
buffer.order(ByteOrder.LITTLE_ENDIAN);
|
return buffer.getFloat(pos);
|
}
|
public static double buf2Double(byte[] Buf, int pos)
|
{
|
LastErr=0;
|
ByteBuffer buffer = ByteBuffer.wrap(Buf);
|
buffer.order(ByteOrder.LITTLE_ENDIAN);
|
return buffer.getDouble(pos);
|
}
|
|
private static byte [] GetVailStringBuf(byte[] Buf, int pos)
|
{
|
LastErr=0;
|
int n;
|
for(n=pos;n<Buf.length;n++)
|
{
|
if(Buf[n]==0)break;
|
}
|
byte tmp []=new byte [n];
|
System.arraycopy(Buf, pos,tmp,0,n);
|
return tmp;
|
}
|
public static String buf2String(byte[] Buf, int pos)
|
{
|
LastErr=0;
|
byte tmp []=GetVailStringBuf(Buf,pos);
|
return new String(tmp);
|
}
|
public static void buf2VarArray(boolean[] OutDataArray, byte[] Buf, int pos, int arrSize, int typeSize)
|
{
|
for (int n = 0; n < arrSize; n++)
|
{
|
OutDataArray[n]=buf2Bool( Buf, pos);
|
pos = pos + typeSize;
|
}
|
}
|
public static void buf2VarArray(char[] OutDataArray,byte[] Buf, int pos,int arrSize,int typeSize)
|
{
|
for (int n = 0; n < arrSize; n++)
|
{
|
OutDataArray[n]=buf2Char(Buf, pos);
|
pos = pos + typeSize;
|
}
|
}
|
public static void buf2VarArray(byte[] OutDataArray, byte[] Buf, int pos, int arrSize, int typeSize)
|
{
|
for (int n = 0; n < arrSize; n++)
|
{
|
OutDataArray[n]=buf2Byte(Buf, pos);
|
pos = pos + typeSize;
|
}
|
}
|
public static void buf2VarArray(short[] OutDataArray, byte[] Buf, int pos, int arrSize, int typeSize)
|
{;
|
for (int n = 0; n < arrSize; n++)
|
{
|
OutDataArray[n]=buf2Short(Buf, pos);
|
pos = pos + typeSize;
|
}
|
}
|
public static void buf2VarArray(int [] OutDataArray, byte[] Buf, int pos, int arrSize, int typeSize)
|
{
|
for (int n = 0; n < arrSize; n++)
|
{
|
OutDataArray[n]=buf2Int(Buf, pos);
|
pos = pos + typeSize;
|
}
|
}
|
public static void buf2VarArray(float[] OutDataArray, byte[] Buf, int pos, int arrSize, int typeSize)
|
{
|
for (int n = 0; n < arrSize; n++)
|
{
|
OutDataArray[n]=buf2Float(Buf, pos);
|
pos = pos + typeSize;
|
}
|
}
|
public static void buf2VarArray(double[] OutDataArray, byte[] Buf, int pos, int arrSize, int typeSize)
|
{
|
for (int n = 0; n < arrSize; n++)
|
{
|
OutDataArray[n]=buf2Double(Buf, pos);
|
pos = pos + typeSize;
|
}
|
}
|
public static void buf2VarArray(String[] OutDataArray, byte[] Buf, int pos, int arrSize, int typeSize)
|
{
|
for (int n = 0; n < arrSize; n++)
|
{
|
OutDataArray[n]=buf2String(Buf, pos);
|
pos = pos + typeSize;
|
}
|
}
|
|
}
|
|