Java Long to Byte Array longToByteArray6(long addr)

Here you can find the source of longToByteArray6(long addr)

Description

Converts a long number to a 6 bytes array for MAC addresses.

License

Open Source License

Parameter

Parameter Description
addr The long number.

Return

The byte array.

Declaration

public static byte[] longToByteArray6(long addr) 

Method Source Code

//package com.java2s;
/*//  ww  w .  ja v a2  s  .c  o  m
 * Copyright (c) 2013-2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

public class Main {
    /**
     * Constant holding the number of bits in a byte
     */
    public static final int NumBitsInAByte = 8;
    /**
     * Constant holding the number of bytes in MAC Address
     */
    public static final int MACAddrLengthInBytes = 6;

    /**
     * Converts a long number to a 6 bytes array for MAC addresses.
     *
     * @param addr The long number.
     * @return The byte array.
     */
    public static byte[] longToByteArray6(long addr) {
        byte[] mac = new byte[MACAddrLengthInBytes];
        int i = MACAddrLengthInBytes - 1;
        do {
            mac[i] = (byte) addr;
            addr >>>= NumBitsInAByte;
            i--;
        } while (i >= 0);
        return mac;
    }
}

Related

  1. longToByteArray(long value, byte[] byteArray)
  2. longToByteArray(long value, byte[] data, int offset)
  3. longToByteArray(long value, byte[] dest)
  4. longToByteArray(long value, int nrOfBytes)
  5. longToByteArray6(long addr)
  6. longToByteArrayBE(long data)
  7. longToByteArrayForAS(long i)
  8. longToByteBounds(long value)
  9. longToBytes(byte[] arr, int offset, long num)