0
我有一個方法,它從一個字符串中獲取一個MAC地址並將其轉換爲一個字節數組。然後我有另一種方法採取該字節數組並將其轉換爲長。如下長MAC地址回到字節數組
final long address = ((long)addr[5] & 0xff)
+ (((long)addr[4] & 0xff) << 8)
+ (((long)addr[3] & 0xff) << 16)
+ (((long)addr[2] & 0xff) << 24)
+ (((long)addr[1] & 0xff) << 32)
+ (((long)addr[0] & 0xff) << 40);
但是我不知道如何將這個長轉換回字節數組存儲。
我已經試過
/* long l = mac in long format address | mac addresses only take 6 bytes */
buffer[currentOffset++] = (byte)l;
buffer[currentOffset++] = (byte)(l >> 8);
buffer[currentOffset++] = (byte)(l >> 16);
buffer[currentOffset++] = (byte)(l >> 24);
buffer[currentOffset++] = (byte)(l >> 32);
buffer[currentOffset++] = (byte)(l >> 40);
它不轉換回雖然正確。
注意,[字節緩衝區](http://download.oracle.com/javase /6/docs/api/java/nio/ByteBuffer.html)類型包含執行此類操作的方法。 – McDowell 2011-05-21 11:07:31