How to convert byte[] to Byte[] and the other way around?

前端 未结 7 691
小鲜肉
小鲜肉 2020-12-02 10:18

How to convert byte[] to Byte[] and also Byte[] to byte[], in the case of not using any 3rd party library?

Is the

7条回答
  •  伪装坚强ぢ
    2020-12-02 10:41

    Byte class is a wrapper for the primitive byte. This should do the work:

    byte[] bytes = new byte[10];
    Byte[] byteObjects = new Byte[bytes.length];
    
    int i=0;    
    // Associating Byte array values with bytes. (byte[] to Byte[])
    for(byte b: bytes)
       byteObjects[i++] = b;  // Autoboxing.
    
    ....
    
    int j=0;
    // Unboxing Byte values. (Byte[] to byte[])
    for(Byte b: byteObjects)
        bytes[j++] = b.byteValue();
    

提交回复
热议问题