Java Long to longToBaseCode(char[] target, int targetOffset, long value, int base, int lengthLimit, boolean fillZeros, boolean upperCase)

Here you can find the source of longToBaseCode(char[] target, int targetOffset, long value, int base, int lengthLimit, boolean fillZeros, boolean upperCase)

Description

Converts long value to code of given base and length limit.

License

Apache License

Parameter

Parameter Description
target target characters array
targetOffset offset position in target array
value value
base target numerical base, supported values are 1 to 16
lengthLimit length limit
fillZeros flag if rest of the value should be filled with zeros
upperCase upper case for values greater than 9

Return

offset of characters position

Declaration

public static int longToBaseCode(char[] target, int targetOffset,
        long value, int base, int lengthLimit, boolean fillZeros,
        boolean upperCase) 

Method Source Code

//package com.java2s;
/*//from ww  w  .  j a  v a2 s  . co  m
 * Copyright (C) ExBin Project
 *
 * 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 {
    public static final char[] UPPER_HEX_CODES = "0123456789ABCDEF"
            .toCharArray();
    public static final char[] LOWER_HEX_CODES = "0123456789abcdef"
            .toCharArray();

    /**
     * Converts long value to code of given base and length limit.
     *
     * Optionally fills rest of the value with zeros.
     *
     * @param target target characters array
     * @param targetOffset offset position in target array
     * @param value value
     * @param base target numerical base, supported values are 1 to 16
     * @param lengthLimit length limit
     * @param fillZeros flag if rest of the value should be filled with zeros
     * @param upperCase upper case for values greater than 9
     * @return offset of characters position
     */
    public static int longToBaseCode(char[] target, int targetOffset,
            long value, int base, int lengthLimit, boolean fillZeros,
            boolean upperCase) {
        char[] codes = upperCase ? UPPER_HEX_CODES : LOWER_HEX_CODES;
        for (int i = lengthLimit - 1; i >= 0; i--) {
            target[targetOffset + i] = codes[(int) (value % base)];
            if (!fillZeros && value == 0) {
                return i;
            }
            value = value / base;
        }

        return 0;
    }
}

Related

  1. long2minLeb(final long x)
  2. longTo36Str(String str)
  3. longTo4ByteArray(byte[] bytes, int offset, long value)
  4. longTo4LengthBytes(long num)
  5. LongToAscii(long number, byte[] buf, int offset, int length)
  6. longToBasicType(long l, Class clazz)
  7. longToBcd(long num, int size)
  8. longToBcd(long src, int len, int flag)
  9. longToBigEndian(long value, byte[] in, int offset)