Java Long to String LongToString(long value)

Here you can find the source of LongToString(long value)

Description

Long To String

License

Creative Commons License

Declaration

public static String LongToString(long value) 

Method Source Code

//package com.java2s;

public class Main {
    private static final String HexAlphabet = "0123456789ABCDEF";

    public static String LongToString(long value) {
        if (value == Long.MIN_VALUE) {
            return "-9223372036854775808";
        }/*from   w  w w .jav  a  2s  .c o m*/
        if (value == 0) {
            return "0";
        }
        boolean neg = value < 0;
        char[] chars = new char[24];
        int count = 0;
        if (neg) {
            chars[0] = '-';
            ++count;
            value = -value;
        }
        while (value != 0) {
            char digit = HexAlphabet.charAt((int) (value % 10));
            chars[count++] = digit;
            value /= 10;
        }
        if (neg) {
            ReverseChars(chars, 1, count - 1);
        } else {
            ReverseChars(chars, 0, count);
        }
        return new String(chars, 0, count);
    }

    private static void ReverseChars(char[] chars, int offset, int length) {
        int half = length >> 1;
        int right = offset + length - 1;
        for (int i = 0; i < half; i++, right--) {
            char value = chars[offset + i];
            chars[offset + i] = chars[right];
            chars[right] = value;
        }
    }
}

Related

  1. longToString(long longValue, boolean noCase)
  2. longToString(long n)
  3. LongToString(long number, boolean bcdFlag, int bufferSize)
  4. longToString(long val)
  5. longToString(Long value)
  6. longToString(long value)
  7. longToString(long value)
  8. longToString(long value)
  9. longToString(long[] values)