Integer and Long do not correctly implement unsigned toStrings
Alexandre Oliva
oliva at dcc.unicamp.br
Tue Sep 8 22:44:53 PDT 1998
--Multipart_Wed_Sep__9_05:44:54_1998-1
Content-Type: text/plain; charset=US-ASCII
The JLS mandates that toHexString, toOctalString and toBinaryString
use ints and longs as unsigned values. The attached patch provides
correct implementations for these methods.
--
Alexandre Oliva
mailto:oliva at dcc.unicamp.br mailto:aoliva at acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
--Multipart_Wed_Sep__9_05:44:54_1998-1
Content-Type: application/octet-stream; type=patch
Content-Disposition: attachment; filename="unsigned.toString.diff"
Content-Transfer-Encoding: 7bit
Alexandre Oliva <oliva at dcc.unicamp.br>
* libraries/javalib/java/lang/Integer.java (toUnsignedString):
create string from unsigned int, using bit operations
(toBinaryString, toHexString, toOctalString): implement in terms
of toUnsignedString, as per the Standard
* libraries/javalib/java/lang/Long.java (toUnsignedString):
create string from unsigned long, using bit operations
(toBinaryString, toHexString, toOctalString): implement in terms
of toUnsignedString, as per the Standard
diff -u ../guarana/kaffe/libraries/javalib/java/lang/Integer.java libraries/javalib/java/lang/Integer.java
--- ../guarana/kaffe/libraries/javalib/java/lang/Integer.java Thu Aug 20 00:13:43 1998
+++ libraries/javalib/java/lang/Integer.java Wed Sep 9 05:37:37 1998
@@ -145,16 +145,32 @@
}
}
+private static String toUnsignedString(int i, int bits) {
+ if (i == 0) {
+ return ("0");
+ }
+
+ StringBuffer buf = new StringBuffer();
+ int radix = 1 << bits, mask = radix-1;
+ while (i != 0) {
+ char digit = Character.forDigit(i & mask, radix);
+ i >>>= bits;
+ buf.append(digit);
+ }
+ buf.reverse();
+ return (buf.toString());
+}
+
public static String toBinaryString(int i) {
- return toString(i, 2);
+ return toUnsignedString(i, 1);
}
public static String toHexString(int i) {
- return toString(i, 16);
+ return toUnsignedString(i, 4);
}
public static String toOctalString(int i) {
- return toString(i, 8);
+ return toUnsignedString(i, 3);
}
public String toString()
diff -u ../guarana/kaffe/libraries/javalib/java/lang/Long.java libraries/javalib/java/lang/Long.java
--- ../guarana/kaffe/libraries/javalib/java/lang/Long.java Wed Aug 5 22:20:03 1998
+++ libraries/javalib/java/lang/Long.java Wed Sep 9 05:38:56 1998
@@ -138,16 +138,32 @@
}
}
+private static String toUnsignedString(long i, int bits) {
+ if (i == 0) {
+ return ("0");
+ }
+
+ StringBuffer buf = new StringBuffer();
+ long radix = 1 << bits, mask = radix-1;
+ while (i != 0) {
+ char digit = Character.forDigit(i & mask, radix);
+ i >>>= bits;
+ buf.append(digit);
+ }
+ buf.reverse();
+ return (buf.toString());
+}
+
public static String toBinaryString ( long i ) {
- return toString(i, 2);
+ return toUnsignedString(i, 1);
}
public static String toHexString ( long i ) {
- return toString(i, 16);
+ return toUnsignedString(i, 4);
}
public static String toOctalString ( long i ) {
- return toString(i, 8);
+ return toUnsignedString(i, 3);
}
public String toString() {
--Multipart_Wed_Sep__9_05:44:54_1998-1--
--FAA14222.905330991/grande.dcc.unicamp.br--
More information about the kaffe
mailing list