what’s a fast way to convert an Integer into a Byte Array?
e.g. 0xAABBCCDD => {AA, BB, CC, DD}
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.
Have a look at the ByteBuffer class.
Setting the byte order ensures that
result[0] == 0xAA,result[1] == 0xBB,result[2] == 0xCCandresult[3] == 0xDD.Or alternatively, you could do it manually:
The
ByteBufferclass was designed for such dirty hands tasks though. In fact the privatejava.nio.Bitsdefines these helper methods that are used byByteBuffer.putInt():