B
B
bitbuff2018-02-23 20:45:06
Java
bitbuff, 2018-02-23 20:45:06

How to convert int[] to byte[]?

I have an int[] array with elements ranging from 0 to 255. How can I convert this array to a byte array so that it can be decoded to UTF-8 ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex Wells, 2018-02-23
@Alex_Wells

int[] data = { 100, 200, 300, 400 };

ByteBuffer byteBuffer = ByteBuffer.allocate(data.length * 4);        
IntBuffer intBuffer = byteBuffer.asIntBuffer();
intBuffer.put(data);

byte[] array = byteBuffer.array();

PS: stackoverflow

D
Denis Zagaevsky, 2018-02-24
@zagayevskiy

And what prevents you from allocating an array of bytes and writing values ​​there in a loop?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question