Java Hex Calculate toHexN(long src, int hexn)

Here you can find the source of toHexN(long src, int hexn)

Description

to Hex N

License

Apache License

Declaration

public static String toHexN(long src, int hexn) 

Method Source Code

//package com.java2s;
/*//from   ww  w.  jav a2 s  . c o  m
 * Copyright 2015 The RPC Project
 *
 * The RPC Project licenses this file to you 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 {
    final static char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
            'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

    public static String toHexN(long src, int hexn) {
        return toHexN(src, hexn, 13);
    }

    public static String toHexN(long i, int radix, int length) {
        if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
            radix = 10;
        if (radix == 10)
            return Long.toString(i);
        char[] buf = { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
                '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
                '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
                '0', '0', '0', '0', '0', '0', '0' };
        int charPos = 64;
        boolean negative = (i < 0);

        if (!negative) {
            i = -i;
        }

        while (i <= -radix) {
            buf[charPos--] = DIGITS[(int) (-(i % radix))];
            i = i / radix;
        }
        buf[charPos] = DIGITS[(int) (-i)];

        if (negative) {
            buf[--charPos] = '-';
        }

        return new String(buf, 65 - length, length);
    }

    public static String toHexN(int src, int hexn) {
        return toHexN(src, hexn, 6);
    }

    public static String toHexN(int i, int radix, int length) {
        if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
            radix = 10;

        /* Use the faster version */
        if (radix == 10) {
            return Integer.toString(i);
        }

        char buf[] = { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
                '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' };
        boolean negative = (i < 0);
        int charPos = 32;

        if (!negative) {
            i = -i;
        }

        while (i <= -radix) {
            buf[charPos--] = DIGITS[-(i % radix)];
            i = i / radix;
        }
        buf[charPos] = DIGITS[-i];

        if (negative) {
            buf[--charPos] = '-';
        }

        return new String(buf, 33 - length, length);
    }
}

Related

  1. toHexFromBytes(byte[] bytes)
  2. toHexFromBytes(final byte[] bytes)
  3. toHexFromOct(final String octSymbols)
  4. toHexHashString(byte[] id)
  5. toHexLiteral(int v)
  6. ToHexNumber(int c)
  7. toHexOrNegative(int i)
  8. toHexPadZero(byte[] bytes)
  9. toHexShortString(byte[] bytes)