Android Byte Array to Long Convert byteArray2long(final byte[] number, final boolean swapHalfWord)

Here you can find the source of byteArray2long(final byte[] number, final boolean swapHalfWord)

Description

Converts a byte array to a long.

License

Apache License

Parameter

Parameter Description
number An array of bytes containing a number
swapHalfWord Swap halfwords ([0][1][2][3]->[1][0][3][2])

Exception

Parameter Description
UnsupportedOperationException if number length is not a multiple of 2

Return

The long value

Declaration

static long byteArray2long(final byte[] number,
        final boolean swapHalfWord) 

Method Source Code

//package com.java2s;
/*/*from w  w  w . j  ava 2  s.c o m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

public class Main {
    /**
     * Converts a byte array to a long. Halfwords can be swapped by setting
     * swapHalfWord=true.
     * 
     * @param number
     *            An array of bytes containing a number
     * @param swapHalfWord
     *            Swap halfwords ([0][1][2][3]->[1][0][3][2])
     * @return The long value
     * @throws UnsupportedOperationException if number length is not a multiple of 2
     */
    static long byteArray2long(final byte[] number,
            final boolean swapHalfWord) {
        if (number.length % 2 != 0) {
            throw new UnsupportedOperationException();
        }

        long ret = 0;
        int pos = 0;
        byte tmp_number[] = new byte[number.length];
        System.arraycopy(number, 0, tmp_number, 0, number.length);

        if (!swapHalfWord) {
            byte tmp = 0;
            for (pos = 0; pos < tmp_number.length; pos++) {
                tmp = tmp_number[pos];
                tmp_number[pos++] = tmp_number[pos];
                tmp_number[pos] = tmp;
            }
        }

        ret = tmp_number[0] & 0xFF;
        for (pos = 1; pos < tmp_number.length; pos++) {
            ret <<= 8;
            ret |= tmp_number[pos] & 0xFF;
        }
        return ret;
    }
}

Related

  1. makeLong(byte b7, byte b6, byte b5, byte b4, byte b3, byte b2, byte b1, byte b0)
  2. getLong(byte[] src, boolean bigEndin)
  3. uint16ToLong(byte[] buf, int offset)
  4. uint32ToLong(byte[] buf, int offset)
  5. uint64ToLong(byte[] buf, int offset)
  6. parseLong(byte[] array, int offset, int multiplier, long[] result)
  7. parseLong(byte[] array, int offset, long[] result)
  8. readLong(byte[] buff, int pos)