Java Unsigned Number Create unsignedLongToString(long value)

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

Description

Treats the provided long as unsigned and converts it to a string.

License

Apache License

Declaration

private static String unsignedLongToString(long value) 

Method Source Code

//package com.java2s;
/* Copyright (c) 2008 Google Inc.
 *
 * 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./*from   w w  w. j a  va  2  s.  c  o  m*/
 */

public class Main {
    /**
     *  Treats the provided long as unsigned and converts it to a string.
     */
    private static String unsignedLongToString(long value) {
        if (value >= 0) {
            return Long.toString(value);
        } else {
            // Split into two unsigned halves.  As digits are printed out from
            // the bottom half, move data from the top half into the bottom
            // half
            int max_dig = 20;
            char[] cbuf = new char[max_dig];
            int radix = 10;
            int dst = max_dig;
            long top = value >>> 32;
            long bot = value & 0xffffffffl;
            bot += (top % radix) << 32;
            top /= radix;
            while (bot > 0 || top > 0) {
                cbuf[--dst] = Character.forDigit((int) (bot % radix), radix);
                bot = (bot / radix) + ((top % radix) << 32);
                top /= radix;
            }
            return new String(cbuf, dst, max_dig - dst);
        }
    }
}

Related

  1. unsignedLessThan(long a, long b)
  2. unsignedLocalIntersect2by2(final short[] set1, final int length1, final short[] set2, final int length2, final short[] buffer)
  3. unsignedLocalIntersect2by2Cardinality(final short[] set1, final int length1, final short[] set2, final int length2)
  4. unsignedLong(byte b)
  5. unsignedLongToByteArray(final long value)
  6. unsignedLongToString(long x)
  7. unsignedMediumToBytes(final Long unsignedInt)
  8. unsignedNumericToByteArray(long src, int length)
  9. unsignedOneSidedGallopingIntersect2by2(final short[] smallSet, final int smallLength, final short[] largeSet, final int largeLength, final short[] buffer)