//描述:从startPos处将byte[]转换为short型,转换长度为byteLength ==2;public class BytesToShort {
public static short bytesToShortWay(byte[] source, int startPos, int byteLength) {
if(startPos<0 || ( byteLength!=2 ) || source == null
|| source.length < startPos+byteLength-1)//保证转换合理性
return 0;
else
return bytesToshort(source[startPos+0], source[startPos+1], byteLength);
}
public static short bytesToshort(byte b0, byte b1, int byteLength) {//转换
if(byteLength != 2)
return 0;
int i = 0;
short out = 0;
if(b0<0) i = 256+b0; else i = b0;
out += i;
if(b1<0) i = 256+b1; else i = b1;
out += (i<<8);
return out;
}
}
//end
public static short bytesToShortWay(byte[] source, int startPos, int byteLength) {
if(startPos<0 || ( byteLength!=2 ) || source == null
|| source.length < startPos+byteLength-1)//保证转换合理性
return 0;
else
return bytesToshort(source[startPos+0], source[startPos+1], byteLength);
}
public static short bytesToshort(byte b0, byte b1, int byteLength) {//转换
if(byteLength != 2)
return 0;
int i = 0;
short out = 0;
if(b0<0) i = 256+b0; else i = b0;
out += i;
if(b1<0) i = 256+b1; else i = b1;
out += (i<<8);
return out;
}
}
//end
一般我是通过String作为桥梁来处理的
即
float -> String -> byte[]
byte[] -> String -> float
从float到String用toString()方法即可,而String到byte[]也有getByte()方法。
如果能保证byte[]中的是因float变量而来的数据,转为String后造型成float是很简单的事情。至于如何将byte[]转为String,toString()方法肯定不行,需要自写个class处理。下面给你一个
* 描述:将byte类型数据转换成String
* 参数:startPos确定开始转换字节处,byteLength确定转换的长度
public class BytesToString {
public static String bytesToStringWay(byte[] source,
int startPos, int byteLength) {
if(startPos<0 || byteLength<=0 || source == null
|| source.length < startPos+byteLength-1)return null; StringBuffer ret= new StringBuffer(byteLength);//结果字串
char c=' ';
int k=0;
byte b=0;
for(int i=0;i<byteLength;i++)
{
b = source[startPos+i];
if(b<0) k = 256+b; else k = b;
c = (char)k;//change
ret.append(c);
}
//ret.append()
return ret.toString();
}
}
//end
public class Test {
public static void main(String[] args) {
float f0 = 12.12F;
String s0 = Float.toString(f0);//float To String
byte[] b0 = s0.getBytes();//String To byte[]
String s1 = BytesToString.bytesToStringWay(b0,0,b0.length);//byte[] back To String
float f1 = Float.parseFloat(s1);//String back To float
System.out.println(f1);//last look
}
}
class BytesToString {
public static String bytesToStringWay(byte[] source,
int startPos, int byteLength) {
if(startPos<0 || byteLength<=0 || source == null
|| source.length < startPos+byteLength-1)return null; StringBuffer ret= new StringBuffer(byteLength);//结果字串
char c=' ';
int k=0;
byte b=0;
for(int i=0;i<byteLength;i++)
{
b = source[startPos+i];
if(b<0) k = 256+b; else k = b;
c = (char)k;//change
ret.append(c);
}
//ret.append()
return ret.toString();
}
}
DataInputStream
对方是 ALPHA 机, 和 PC 机一样是 低位 在前, 而 SUN 的 JAVA 和SOLARIS 一样是 高位在前, 那么我 换一下位, 用ByteArrayInputStream,
DataInputStream, 解决了这个问题。
我是初次在 CSDN 上发贴子,非常感谢大家的指点。 我 对ORACLE 相当熟悉, UML 也有一定是实用经验。 希望能贡献出自己的知识, 和大家多交流。互相促进。 :) for ( ; i < 4 ; i++) {
b_float[3-i] = buf[head_length+13 + i_no*6 + i];
} try {
in = new DataInputStream( new ByteArrayInputStream( b_float));
f_value = in.readFloat();
和 首先 回复的 Koham(战神有我) ,
和 DrunkenLion(一片绿叶) 提出了较好的解决方案。
以及jimjxr(宝宝猫)