Created
February 25, 2019 09:33
-
-
Save matteo-grella/258f4c6c9be1e87717f0f29e31e37732 to your computer and use it in GitHub Desktop.
Transform an Integer to a Byte Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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