In Java, byte arrays and long arrays are converted to each other.
The byteandlongarraytest code is as follows:
- Package com. array. test;
- Import java. Io. ioexception;
- /**
- * @ Version 1.0
- * @ Author wangwei
- * @ Created on 2008-10-17
- */
- Public class byteandlongarraytest {
- /**
- * Convert a long array to a byte array
- *
- * @ Param longarray
- * @ Return
- * @ Throws ioexception
- */
- Public static byte [] longtobyte (long [] longarray) throws ioexception {
- Byte [] bytearray = new byte [longarray. length * 8];
- For (INT I = 0; I <longarray. length; I ++)
- {
- Bytearray [0 + 8 * I] = (byte) (longarray [I]> 56 );
- Bytearray [1 + 8 * I] = (byte) (longarray [I]> 48 );
- Bytearray [2 + 8 * I] = (byte) (longarray [I]> 40 );
- Bytearray [3 + 8 * I] = (byte) (longarray [I]> 32 );
- Bytearray [4 + 8 * I] = (byte) (longarray [I]> 24 );
- Bytearray [5 + 8 * I] = (byte) (longarray [I]> 16 );
- Bytearray [6 + 8 * I] = (byte) (longarray [I]> 8 );
- Bytearray [7 + 8 * I] = (byte) (longarray [I]> 0 );
- }
- Return bytearray;
- }
- /**
- * Convert byte array to long array
- *
- * @ Param bytearray
- * @ Return
- * @ Throws ioexception
- */
- Public static long [] bytetolong (byte [] bytearray) throws ioexception {
- Long [] longarray = new long [bytearray. Length/8];
- For (INT I = 0; I <longarray. length; I ++)
- {
- Longarray [I] = (long) bytearray [0 + 8 * I] & 0xff) <56)
- | (Long) bytearray [1 + 8 * I] & 0xff) <48)
- | (Long) bytearray [2 + 8 * I] & 0xff) <40)
- | (Long) bytearray [3 + 8 * I] & 0xff) <32)
- | (Long) bytearray [4 + 8 * I] & 0xff) <24)
- | (Long) bytearray [5 + 8 * I] & 0xff) <16)
- | (Long) bytearray [6 + 8 * I] & 0xff) <8)
- | (Long) bytearray [7 + 8 * I] & 0xff) <0 );
- }
- Return longarray;
- }
- /**
- * @ Param ARGs
- */
- Public static void main (string [] ARGs) throws exception {
- // Todo auto-generated method stub
- Long [] longarray = {500000 };
- Byte [] bytearray = longtobyte (longarray );
- For (INT I = 0; I <bytearray. length; I ++)
- {
- System. Out. Print (bytearray [I] + "");
- }
- System. Out. println ();
- Long [] longarray2 = bytetolong (bytearray );
- For (INT I = 0; I <longarray. length; I ++)
- System. Out. Print (longarray [I] + "");
- }
- }