Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6322074

Converting integers to string as if unsigned

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P3 P3
    • 8
    • 6
    • core-libs
    • b25
    • generic
    • generic
    • Verified

      Josh writes:

      Today a colleague of mine needed to translate a long to a string as if
      it were unsigned. java.lang.Long lets you do this in binary, octal,
      or hex, but not decimal. I had to give him this piece of inefficient
      code that leverages BigIntegers string conversion capabilities:

        public static String ulongToString(long i) {
          BigInteger b = BigInteger.valueOf(i & Long.MAX_VALUE);
          if (i < 0)
            b = b.flipBit(Long.SIZE - 1);
          return b.toString();
        }

      Surely we don't want people doing this! As we've discussed over the
      years, Integer, Long, and Short should have a bunch of methods for
      dealing with integeral primitives as if they were unsigned. We don't
      need many. If we could only have one, it would be the one above
      (coded efficiently, of course). But I think we should also have some
      upconversion operations (ushort -> int, uint -> long), arithmetic
      operations (most but not all of which are simple pass-throughs to the
      signed operations), and comparison operations (which rely on the ^
      MAX_VALUE trick). Can we please have these in Dolphin? I am willing
      to write them.

            darcy Joe Darcy
            martin Martin Buchholz
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: