I need to convert a byte[] of size 4 to type long for my Android app. I found a function byteArrayToLong(byte_array) which is in the java.io.Bits package, but it isn’t working in my application.
Are there any other alternative functions?
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.
Use
java.nio.ByteBuffer:Edit
Of course, @Kaito is right: you need an array of 8 bytes instead. Also, as others mentioned, you may choose an order in which your bytes will be read (
BIG_ENDIANby default):Also, why to prefer an API method? For the sake of simplicity: instead of recognizing byte to long conversion in bitwise shifts you can read what happens here in plain English.