Skip to content

Instantly share code, notes, and snippets.

@matteo-grella
Created February 25, 2019 09:33
Show Gist options
  • Save matteo-grella/258f4c6c9be1e87717f0f29e31e37732 to your computer and use it in GitHub Desktop.
Save matteo-grella/258f4c6c9be1e87717f0f29e31e37732 to your computer and use it in GitHub Desktop.
Transform an Integer to a Byte Array
/**
* Transform an Integer to a Byte Array.
*
* @param value the Integer
*
* @return the Byte Array
*/
internal fun getByteArrayFromInt(value: Int): ByteArray {
val mask = 0xFF // binary 1111 1111
var number = value
val result = ByteArray(java.lang.Integer.BYTES) { 0 }
for (i in 0 until result.size) {
result[i] = number.and(mask).toByte()
number = number.shr(8)
}
result.reverse()
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment