Skip to content

Instantly share code, notes, and snippets.

@Farbklex
Last active February 18, 2020 15:00
Show Gist options
  • Save Farbklex/d7078c834c91618f7814bfd6fbfe7e07 to your computer and use it in GitHub Desktop.
Save Farbklex/d7078c834c91618f7814bfd6fbfe7e07 to your computer and use it in GitHub Desktop.
Convert an Int to an array or list of Byte with a fixed length (int to byteArray with padding). Useful when a protocol requires you to allocate multiple bytes for one value but you don't need all of them to represent it.
// Convert size int to a list of bytes
val intValue: Int = 4
val intAsBytes = ByteBuffer.allocate(4).putInt(size).array().toList()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment