public class TestFloat { public static byte[] convertHexStringToBytes(String buff) throws Exception { try { buff = buff.replaceAll("\n", " "); StringTokenizer st = new StringTokenizer(buff, " "); int size = st.countTokens(); ByteBuffer bb = ByteBuffer.allocate(size); while (st.hasMoreTokens()) { buff = st.nextToken(); int c = Character.digit(buff.charAt(0), 16) << 4; c = c + Character.digit(buff.charAt(1), 16); bb.put((byte) c); } return bb.array(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); throw new Exception("read file error"); } }
public static void main(String args[]) throws Exception{ byte[] b = TestFloat.convertHexStringToBytes("cd cc cc 3e"); ByteBuffer bb = ByteBuffer.wrap(b); bb.order(ByteOrder.LITTLE_ENDIAN); float f = bb.getFloat(); System.out.println(f);
public class TestFloat {
public static byte[] convertHexStringToBytes(String buff) throws Exception {
try { buff = buff.replaceAll("\n", " ");
StringTokenizer st = new StringTokenizer(buff, " ");
int size = st.countTokens();
ByteBuffer bb = ByteBuffer.allocate(size);
while (st.hasMoreTokens()) {
buff = st.nextToken();
int c = Character.digit(buff.charAt(0), 16) << 4;
c = c + Character.digit(buff.charAt(1), 16);
bb.put((byte) c);
}
return bb.array();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
throw new Exception("read file error");
}
}
public static void main(String args[]) throws Exception{
byte[] b = TestFloat.convertHexStringToBytes("cd cc cc 3e");
ByteBuffer bb = ByteBuffer.wrap(b);
bb.order(ByteOrder.LITTLE_ENDIAN);
float f = bb.getFloat();
System.out.println(f);
}
}
想了个蠢办法
Int型和byte数组,以及long型和byte数组之间的相互转换
public class Hex2Float{
public static int getInt(char ch){
if(ch>='0' && ch<= '9')return ch-='0';
if(ch>='a' && ch<= 'z')ch-=32;
if(ch<'A' || ch> 'Z')throw new RuntimeException("参数不合法!");
switch(ch){
case 'A':return 10;
case 'B':return 11;
case 'C':return 12;
case 'D':return 13;
case 'E':return 14;
case 'F':return 15;
}
return 0;
}
public static char[] str2BitArray(String str){
char chs[]=str.toCharArray();
char bits[]=new char[chs.length*4];
int index=0;
for(int i=0;i<chs.length;i++){
for(int j=3;j>=0;j--){
if(((1<<j)&getInt(chs[i]))!=0){
bits[index++]='1';
}else bits[index++]='0';
}
}
System.out.println(Arrays.toString(bits));
return bits;
}
public static int parseSign(char ch[]){
return (ch[0]=='1'?-1:1);
}
public static int parseExponent(char ch[]){
int result=0;
for(int i=1;i<=8;i++){
result+=(ch[i]-'0')*Math.pow(2,8-i);
}
//System.out.println(result);
return result;
}
public static double parseEnding(char[] ch){
double result=0;
for(int i=9;i<=31;i++){
result+=(ch[i]-'0')*Math.pow(0.5,i-8);
}
//System.out.println(result);
return result;
}
public static float getFloat(String str){
char[] bits=str2BitArray(str);
int sign=parseSign(bits);
int e=parseExponent(bits);
double m=parseEnding(bits);
System.out.println("sign="+sign+",e="+e+",m="+m);
if(e==0 && m==0){
return 0;
}else if((e==0) && (m!=0)){
return (float)(sign*Math.pow(2.0,-126)*m);
}else if(e>=1 && e<=254 && m!=0){
return (float)(sign*Math.pow(2.0,e-127)*(1+m));
}else if((e==255) && m!=0){
if((sign==1) && (m==0.5))return Float.NaN;
else if((sign==1) && (m==0))return Float.NEGATIVE_INFINITY;
else return Float.POSITIVE_INFINITY;
}
return 0;
}
public static void main(String rags[]){
String str="3ecccccd";
float f=getFloat(str);
System.out.println("f="+f);
}
}
System.out.println(Integer.toString(Float.floatToIntBits(d),16));
union int2float
{
int intValue;
float floatValue;
};对一个赋值,使用另一个读出来即可。
bb.order(ByteOrder.LITTLE_ENDIAN);