Java Number Convert numToBytes(byte[] buffer, long value)

Here you can find the source of numToBytes(byte[] buffer, long value)

Description

Place a string representation of a long in an array of bytes without incurring allocation

License

Open Source License

Parameter

Parameter Description
buffer The byte array
value The long to convert

Return

The length of the string representation of the integer -1 indicates some problem (e.g the char buffer was too small)

Declaration

public static int numToBytes(byte[] buffer, long value) 

Method Source Code

//package com.java2s;
/*//  w w  w  .  j  ava2s.com
 *  This file is part of the Jikes RVM project (http://jikesrvm.org).
 *
 *  This file is licensed to You under the Eclipse Public License (EPL);
 *  You may not use this file except in compliance with the License. You
 *  may obtain a copy of the License at
 *
 *      http://www.opensource.org/licenses/eclipse-1.0.php
 *
 *  See the COPYRIGHT.txt file distributed with this work for information
 *  regarding copyright ownership.
 */

public class Main {
    /**
     * Place a string representation of a long in an array of bytes
     * without incurring allocation
     *
     * @param buffer The byte array
     * @param value The long to convert
     * @return The length of the string representation of the integer
     *         -1 indicates some problem (e.g the char buffer was too small)
     */
    public static int numToBytes(byte[] buffer, long value) {
        return numToBytes(buffer, value, 10);
    }

    /**
     * Place a string representation of a long in an array of bytes
     * without incurring allocation
     *
     * @param buffer The byte array
     * @param value The long to convert
     * @param radix the base to use for conversion
     * @return The length of the string representation of the integer
     *         -1 indicates some problem (e.g the char buffer was too small)
     */
    public static int numToBytes(byte[] buffer, long value, int radix) {

        if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
            radix = 10;

        if (value == 0) {
            buffer[0] = (byte) '0';
            return 1;
        }

        boolean negative;
        long longValue;
        int count;
        if (!(negative = (value < 0))) {
            longValue = -value;
            count = 1;
        } else {
            longValue = value;
            count = 2;
        }

        long j = longValue;
        while ((j /= radix) != 0)
            count++;
        if (count > buffer.length)
            return -1; // overflow

        int i = count;
        do {
            int ch = (int) -(longValue % radix);
            if (ch > 9)
                ch -= (10 - 'a');
            else
                ch += '0';
            buffer[--i] = (byte) ch;
        } while ((longValue /= radix) != 0);
        if (negative)
            buffer[0] = (byte) '-';

        return count;

    }
}

Related

  1. convertToLong(String decimal, int precision)
  2. ConvertToMeters(double distance)
  3. convertToMm(double value)
  4. convertToMoney(float money)
  5. getMoneyConversion(int money, int divisor)
  6. numToBytes(byte[] buffer, long value, int radix)
  7. numToBytes(long value, byte[] b, int off, int len)
  8. numToInt(Number n)
  9. numToIPv4(StringBuilder host)