Java Long Number From longFromLex(byte[] bytes)

Here you can find the source of longFromLex(byte[] bytes)

Description

Converts lexicographical sortable long to a regular java long

License

Apache License

Declaration

public static long longFromLex(byte[] bytes) 

Method Source Code

//package com.java2s;
/*//from   ww w  .  ja  v a 2 s  .  co m
 * Copyright 2013 Jive Software, Inc
 *
 * Licensed 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 {
    static private final long c64 = 0x8000000000000000L;

    /**
     * Converts lexicographical sortable long to a regular java long
     *
     * @return
     */
    public static long longFromLex(byte[] bytes) {
        long v = bytesLong(bytes, 0);
        v ^= c64;
        return v;
    }

    /**
     * Converts lexicographical sortable long to a regular java long
     *
     * @return
     */
    public static long longFromLex(byte[] bytes, int offset) {
        long v = bytesLong(bytes, offset);
        v ^= c64;
        return v;
    }

    /**
     *
     * @param bytes
     * @param _offset
     * @return
     */
    public static long bytesLong(byte[] bytes, int _offset) {
        if (bytes == null) {
            return 0;
        }
        long v = 0;
        v |= (bytes[_offset + 0] & 0xFF);
        v <<= 8;
        v |= (bytes[_offset + 1] & 0xFF);
        v <<= 8;
        v |= (bytes[_offset + 2] & 0xFF);
        v <<= 8;
        v |= (bytes[_offset + 3] & 0xFF);
        v <<= 8;
        v |= (bytes[_offset + 4] & 0xFF);
        v <<= 8;
        v |= (bytes[_offset + 5] & 0xFF);
        v <<= 8;
        v |= (bytes[_offset + 6] & 0xFF);
        v <<= 8;
        v |= (bytes[_offset + 7] & 0xFF);
        return v;
    }
}

Related

  1. longFrombytes(byte[] b, int pos)
  2. longFromBytes(byte[] buffer, int offset)
  3. longFromBytes(byte[] bytes, int index)
  4. longFromBytes(byte[] bytes, int offset)
  5. longFromDB(String dbLong)
  6. longFromObject(Object o)
  7. toLongAsObject(Object obj)
  8. toLongBE(byte[] b, int i)
  9. toLongBE(byte[] src, int offset)