Here you can find the source of longToBaseCode(char[] target, int targetOffset, long value, int base, int lengthLimit, boolean fillZeros, boolean upperCase)
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 |
public static int longToBaseCode(char[] target, int targetOffset, long value, int base, int lengthLimit, boolean fillZeros, boolean upperCase)
//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; } }