2012-10-09 65 views

回答

6

使用ByteBuffer

val arr = listOfLongs. 
    foldLeft(ByteBuffer.allocate(8 * listOfLongs.size)){ (buffer, lon) => 
    buffer putLong lon 
    }.array 

或者更勢在必行:

val buffer = ByteBuffer.allocate(8 * listOfLongs.size) 
listOfLongs.foreach(buffer putLong _) 
val arr = buffer.array 

注意:如果你需要小端,只要致電:

buffer.order(java.nio.ByteOrder.LITTLE_ENDIAN) 

開頭。更多靈感來源:Convert long to byte array and add it to another array

相關問題