Java Long Number Create toLong(byte[] value)

Here you can find the source of toLong(byte[] value)

Description

Converte um texto representado por um arranjo de bytes em um inteiro.

License

Apache License

Parameter

Parameter Description
value arranjo.

Return

inteiro.

Declaration

public static long toLong(byte[] value) 

Method Source Code

//package com.java2s;
/*/*ww  w . j a v a 2 s. co  m*/
 * BRCache http://brcache.brandao.org/
 * Copyright (C) 2015 Afonso Brandao. (afonso.rbn@gmail.com)
 *
 * 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 {
    private static final long NEGATIVE_LONG = 0xffffffffffffffffL;
    private static final byte ZERO = '0';
    private static final byte NEGATIVE = '-';
    private static final byte POSITIVE = '+';
    private static final byte TRUE = 1;
    private static final byte FALSE = 0;

    /**
     * Converte um texto representado por um arranjo de bytes em um inteiro.
     * @param value arranjo.
     * @return inteiro.
     */
    public static long toLong(byte[] value) {
        int limit = value.length - 1;
        byte signal = value[0] == NEGATIVE ? FALSE : TRUE;
        byte hasSignal = value[0] == NEGATIVE || value[0] == POSITIVE ? TRUE : FALSE;

        int start = hasSignal == TRUE ? 1 : 0;
        long result = 0;
        int mult = 1;

        for (int i = limit; i >= start; i--) {
            int tmp = value[i] - ZERO;
            result += tmp * mult;
            mult *= 10;
        }

        if (signal == FALSE) {
            result = (result ^ NEGATIVE_LONG) + 1;
        }

        return result;
    }
}

Related

  1. toLong(byte[] readBuffer, int o)
  2. toLong(byte[] si, boolean isReverseOrder)
  3. toLong(byte[] src)
  4. toLong(byte[] src, int srcPos)
  5. toLong(byte[] v)
  6. toLong(byte[] value)
  7. toLong(byte[] vint)
  8. toLong(double val)
  9. toLong(double[] in, long[] out)