如何将一 float 型数据转换为 byte[] 字节数组?(50分)

  • 主题发起人 主题发起人 Hank
  • 开始时间 开始时间
H

Hank

Unregistered / Unconfirmed
GUEST, unregistred user!
下面这段程序错在那里?
代码:
    public byte[] floatToBytes(float f)
    {
        try
        {
            byte abyte0[] = new byte[4];
            abyte0[0] = (byte)(int)f;
            abyte0[1] = (byte)(int)((long)f >> 8 &
-1L);
            abyte0[2] = (byte)(int)((long)f >> 16 &
-1L);
            abyte0[3] = (byte)(int)((long)f >> 24 &
-1L);
            return abyte0;
        }
        catch (NullPointerException _ex2)
        {
            return null;
        }
    }
先谢谢了!
 
看看Float类有没有提供getbytes的方法。
String类是有的。
 
To 小猪
没有。好像提供 getBytes 的也就 String 类.
 
>>byte abyte0[] = new byte[4];
应为 byte* abyte0 = new byte[4];
 
To sw
Java里好像不能这样的。在Java里上面的定义是没问题的,不过还是谢谢你的回答!
谢谢你!
 
var a[1..4]of byte;
f:float;
for i:=1 to 4do

move(f,a,sizeof(f));
 
To ugvanxk
不好意思,我说的是Java中,不是Delphi. 还有,您这段代码好像也有点问题。
 
to Hank:
Sorry!Sorry!Sorry!
我没用过Java,没看出来,实在对不起,Java中是没有指针的
实在对不起!!
 
To sw
您太客气了!其实真的是很感谢您的回答。
顺便提前一下,希望得到您的帮助!
 
结果不对,还是程序编译不过?
 
To zhuny
是结果不对。我用上面这段代码将一个 float 数转换成 byte[] 字节数组,然后
用下面的代码做一次逆转换,结果就不对了。(下面的代码已证明是正确的)
代码:
    public float bytesToFloat(byte abyte0[], int i)
    {
        try
        {
            int j = ((abyte0[i + 3] &
0xff) << 24) + ((abyte0[i + 2] &amp;
0xff) << 16) +
                ((abyte0[i + 1] &amp;
0xff) << 8) + (abyte0[i] &amp;
0xff);
            float f = Float.intBitsToFloat(j);
            if ((new Float(f)).isNaN())
                f = 0.0F;
            if (f == 3.402823E+038F)
                f = 0.0F;
            return f;
        }
        catch (NullPointerException _ex2)
        {
            return 0.0F;
        }
    }
 
如果保证 (int)((long)f >> xx &amp;
-1L) 得到的值小于127,结果会怎样呢??
 
To zhuny
为什么要保证 (int)((long)f >> xx &amp;
-1L) 小于127呢? 前面的 (byte) 转换
才是为了保证这一点的。
有一点疑问:(int)f 后的结果是怎样的? 应该是对 f 取整吧? 那 (long)f 呢?
 
SORRY
因为在干活,没看仔细
不过我刚才调了一下你的代码,转换后的结果好象没有什么问题!
测试数据384.00F
显示:abyte0[0]=-128
abyte0[1]=1
abyte0[2]=0
abyte0[3]=0
 
(long)f
f大于一定的数,就有问题!
不过如果f为double,问题就没有了
所以我想,题是否在于你的foat???
 
To zhuny
不对。 (long)f 转换后,把小数点后面的值全部都去掉了。然后再移位、与,结果
显然是不对的。
不知您有没有什么好的方法能够将一个 float 型的数值转换为四个字节的 byte[]
数组,然后再转换回来?
 
Thank you all! 使用 DataOutput.writeFloat 和 DataInput.readFloat 的方法,
我已经搞定了,源码如下:
代码:
    public byte[] intToBytes(int i)    
    {
        try
        {
            byte[] aBytes = new byte[4];
            aBytes[0] = (byte)i;
            aBytes[1] = (byte)(i >> 8 &amp;
0xff);
            aBytes[2] = (byte)(i >> 16 &amp;
0xff);
            aBytes[3] = (byte)(i >> 24 &amp;
0xff);
            return aBytes;
        }
        catch (NullPointerException _ex2)
        {
            return null;
        }
    }
    
    public int bytesToInt(byte[] aBytes, int i)
    {
        try
        {
            int j = ((aBytes[i + 3] &amp;
0xff) << 24) + ((aBytes[i + 2] &amp;
0xff) << 16) +  ((aBytes[i + 1] &amp;
0xff) << 8) + (aBytes[i] &amp;
0xff);
            if (j == 0x7fffffff)
                    j = 0;
            return j;
        }
        catch (NullPointerException _ex2)
        {
            return 0;
        }
    }            
    public byte[] floatToBytes(float f)
    {
        try
        {
            byte[] aBytes=new byte[4];
            int temp=Float.floatToIntBits(f);
            aBytes=intToBytes(temp);
            return aBytes;
         }
         catch(Exception _ex)
         {
            return null;
         }
    }
    
    public float bytesToFloat(byte[] aBytes, int i)
    {
        try
        {
            int j = bytesToInt(aBytes, i);
            float f = Float.intBitsToFloat(j);
            if ((new Float(f)).isNaN())
                f = 0.0F;
            if (f == 3.402823E+038F)
                f = 0.0F;
            return f;
        }
        catch (NullPointerException _ex2)
        {
            return 0.0F;
        }
    }
不过,还有一个疑问,希望能得到您的帮助:
在上面的转换过程中,一个 float 型的数值经过了以下的转换:
float -> int -> byte[] -> int -> float
但是,系统是如何保证在整个转换过程中 float 类型的数值不变呢?
 
Oh, yeah, I got it. There is a very nice explanation in the JBuilder's help on
this issue.
floatToIntBits
public static int floatToIntBits(float value)
Returns the bit represention of a single-float value. The result is a representation of the floating-point argument
according to the IEEE 754 floating-point "single precision" bit layout.
Bit 31 (the bit that is selected by the mask 0x80000000) represents the sign of the floating-point number.
Bits 30-23 (the bits that are selected by the mask 0x7f800000) represent the exponent.
Bits 22-0 (the bits that are selected by the mask 0x007fffff) represent the significand (sometimes called the
mantissa) of the floating-point number.
If the argument is positive infinity, the result is 0x7f800000.
If the argument is negative infinity, the result is 0xff800000.
If the argument is NaN, the result is 0x7fc00000.
In all cases, the result is an integer that, when given to the intBitsToFloat(int) method, will produce a floating-point
value equal to the argument to floatToIntBits.
Parameters:value - a floating-point number.
Returns:the bits that represent the floating-point number
Thank you all for your help. The points will be hand over soon. :-)
 
接受答案了.
 
后退
顶部