数据类型转换
package com.coretech.tooler; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.nio.IntBuffer; import com.coretech.defobject.Constant; public class DataCovert { //float转byte[] public static void floatToByte(float fArr[],byte[] bRetData,int nOffset) { int uLen = fArr.length * 4; ByteBuffer bb = ByteBuffer.allocate(uLen); byte temp[] = new byte[4]; for (int i = 0; i < fArr.length; i++) { FloatBuffer fb = bb.asFloatBuffer(); fb.put(fArr[i]); bb.get(temp); for (int j = 0; j < 4; j++) { bRetData[4*i + j + nOffset] = temp[3-j]; } } } //byte[]转float public static float[] byteToFloat(byte[] btArr,int nOffset,int ufLen){ float fRet[] = new float[ufLen ]; byte[] bArr = new byte [4]; for (int i = 0; i < ufLen ; i++) { for (int j = 0; j < 4; j++) { bArr[j] = btArr[4*(i+1) - j -1 + nOffset]; } ByteBuffer bb = ByteBuffer.wrap(bArr); FloatBuffer fb = bb.asFloatBuffer(); fRet[i] = fb.get(); } return fRet; } //int转byte[] public static void intToByte(int nArr[],byte[] bRetData,int nOffset) { int uLen = nArr.length * 4; ByteBuffer bb = ByteBuffer.allocate(uLen); byte temp[] = new byte[4]; for (int i = 0; i < nArr.length; i++) { IntBuffer fb = bb.asIntBuffer(); fb.put(nArr[i]); bb.get(temp); for (int j = 0; j < 4; j++) { bRetData[4*i + j + nOffset] = temp[3-j]; } } } //byte[]转int public static int[] byteToInt(byte[] btArr,int nOffset,int ufLen){ int fRet[] = new int[ufLen ]; byte[] bArr = new byte [4]; for (int i = 0; i < ufLen ; i++) { for (int j = 0; j < 4; j++) { bArr[j] = btArr[4*(i+1) - j -1 + nOffset]; } ByteBuffer bb = ByteBuffer.wrap(bArr); IntBuffer fb = bb.asIntBuffer(); fRet[i] = fb.get(); } return fRet; } }
posted on 2014-04-14 09:24 strangeman 阅读(148) 评论(0) 编辑 收藏 举报