Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How java Converts byte [4] to float
How java Converts byte [4] to float
The suggested answer is to incorrectly equate bytes with characters.

byte b[]={0x4 1,(byte) 0xc8,0,0 };

//method 1 stream input, suitable for ME/SE environment.

//The default number is large. If the number is small, you can flip the array first.

data inputstream dis = new data inputstream(new bytearray inputstream(b));

float f = dis . read float();

dis . close();

system . out . println(f);

//Method 2 caches input, which is suitable for SE/EE environment.

byte buffer buf = byte buffer . allocated direct(4); //Direct cache without extra memory

//buf=buf.order(ByteOrder。 LITTLE _ ENDIAN); //Use this line for the big end and the small end by default.

buf . put(b);

buf . rewind();

float F2 = buf . get float();

system . out . println(F2);

The read floating-point number 25.0 will be displayed.