I am communicating with some device and this device sending me data as unsigned bytes. And I need to convert these bytes to float in Java. Is there any way?
Thank you very much.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes.
Float.intBitsToFloattakes 32 bits as an int and converts it to a float.All you need to do first is convert your four bytes to an
intusing the normal shift and or operations appropriate to the endianness of your data. For example,You can also use
FloatBufferdepending on how you are receiving the data.Note, that "the device [is] sending me data as unsigned bytes" is not true. The data is sending you bytes, and Java represents them as signed bytes. Java does not have an unsigned byte type.
http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html