Thank you John, but don't you have your shift operators confused?
>>> fills the high-order bits with zeros, while >> preserves the sign bit,
so hex[c >> 12] where c = \u8000 would incorrectly give me 0xFFF8, wouldn't it?
Of course, if I add & 0xF for the high nibble also it wouldn't matter.
--Ben
John Cowan wrote:
> Ben Yenko-Martinka wrote:
>
> > So I guess that means there's nothing built into Java, huh? Seems like
> > it would've been a no-brainer to include it as a method in the Character
> > object.
>
> If it had been, it would have been no more efficient than roll-your-own.
> Here's a slight improvement on A.Y.'s code. It avoids object creation except for
> generating the final String, and uses low-level operations rather than method calls.
> It also removes the incorrect use of arithmetic right shift, which would generate
> an error for any character above \u7FFF.
>
> private static final char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
> 'A', 'B', 'C', 'D', 'E', 'F'};
>
> private static char[] hexBuffer = {'\\', 'u', '0', '0', '0', '0'};
>
> public String hexRep(char c)
> {
> hexBuffer[2] = hex[c >> 12];
> hexBuffer[3] = hex[(c >> 8) & 0xF];
> hexBuffer[4] = hex[(c >> 4) & 0xF];
> hexBuffer[5] = hex[c & 0xF];
> return new String(hexBuffer);
> }
>
> --
>
> John Cowan http://www.reutershealth.com jcowan@reutershealth.com
> Schlingt dreifach einen Kreis vom dies! / Schliess eurer Aug vor heiliger Schau
> Den er genoss vom Honig-Tau / Und trank die Milch vom Paradies.
> -- Coleridge (tr. Politzer)
This archive was generated by hypermail 2.1.2 : Tue Jul 10 2001 - 17:20:54 EDT