Can we read a same sequence of bytes as different data-types in java? I was wondering if that is possible. The purpose is to demonstrate the representation of different data-types in memory, specifically Java.
Share
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.
You can convert a
doubleto and fromlongpreserving its binary representation (rather than value) using the following methods:doubleToLongBits(),doubleToRawLongBits()andlongBitsToDouble().You can also use ByteBuffer like this:
which outputs:
See also this post.