OSDN Git Service

Fix Long.toBinaryString, Long.toHexString, and Long.toOctalString for negative values.
authorElliott Hughes <enh@google.com>
Sun, 20 Dec 2009 07:39:53 +0000 (23:39 -0800)
committerElliott Hughes <enh@google.com>
Sun, 20 Dec 2009 07:39:53 +0000 (23:39 -0800)
Although (int) -1 == (long) -1, Integer.toXString produces a shorter result than
Long.toXString should.

libcore/luni/src/main/java/java/lang/Long.java

index 1b28a5a..1c52896 100644 (file)
@@ -396,7 +396,7 @@ public final class Long extends Number implements Comparable<Long> {
      */
     public static String toBinaryString(long v) {
         int i = (int) v;
-        if (i == v) {
+        if (v >= 0 && i == v) {
             return Integer.toBinaryString(i);
         }
 
@@ -422,7 +422,7 @@ public final class Long extends Number implements Comparable<Long> {
      */
     public static String toHexString(long v) {
         int i = (int) v;
-        if (i == v) {
+        if (v >= 0 && i == v) {
             return Integer.toHexString(i);
         }
 
@@ -447,7 +447,7 @@ public final class Long extends Number implements Comparable<Long> {
      */
     public static String toOctalString(long v) {
         int i = (int) v;
-        if (i == v) {
+        if (v >= 0 && i == v) {
             return Integer.toOctalString(i);
         }
         int bufLen = 22;  // Max number of octal digits in a long