Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting an array of bytes to List<Byte>

I could think of these things,

  1. Arrays.asList(byte[]) converts byte[] to List<byte[]>,
  2. looping through byte array and add each element to list

I was just wondering Is there any library function to do that?

like image 959
Eternal Noob Avatar asked Nov 20 '10 07:11

Eternal Noob


2 Answers

Library Apache Commons Lang has ArrayUtils.toObject, which turns a primitive array to a typed object array:

int array[] = { 1, 2, 3 }; List<Integer> list = Arrays.asList(ArrayUtils.toObject(array)); 
like image 184
peenut Avatar answered Sep 21 '22 12:09

peenut


As this post suggests: the guava Bytes class can help out:

byte[] bytes = ... List<Byte> byteList = Bytes.asList(bytes); 
like image 20
Jens Nyman Avatar answered Sep 25 '22 12:09

Jens Nyman