如何将float型转换成4byte型

如何将float型转换成4 byte型  

 

 

例如:8.75 十六进制的数据为 0x00,0x00,0x0c,0x41
1,如何将4 BYTE转换成float型
2. 如何将float型转换成4 byte型

 

一个4 byte 的float数,如何得到这4个byte 的具体数值呢。例如 8.75,他的四个byte

union B_F

{
  unsigned char b[4];
  float f;
} b_f;


int i;
b_f.f=8.75;
for(i=0;i<4;i++)

printf("x\n",b_f.b[i]);
b_f.b[0]=0x00;

b_f.b[1]=0x00;

b_f.b[2]=0x0C;

b_f.b[3]=0x41;


printf("%f\n",b_f.f);

 

本人认为方法一 最好 

方法一  
    
  union   conv  
  {   
       float   f;  
       byte     b[4];  
  }   c;  
    
  c.f   =  1234.44;   
  c.b   就是你要的 
  
    
  方法二:   
       float   f;  
       byte   *   pb  =   (byte*)  (&float);   
    
  方法三:使用c++的引用特性   
       float   f;  
       dword     b  =   (byte&)f;

 

 

 

 

 

如何将byte[] 转换成 float或double??

如何将4 BYTE转换成float型

 

 public static float toFloat(byte[] b)

{ 
      // 4 bytes
      int accum= 0; 
      for ( intshiftBy = 0; shiftBy < 4; shiftBy++ )

{ 
           accum |=(b[shiftBy] & 0xff) << shiftBy * 8; 
     } 
      return Float.intBitsToFloat(accum); 
}

你可能感兴趣的:(如何将float型转换成4byte型)