From: Roedy Green on
On Sat, 26 Dec 2009 00:08:42 -0800 (PST), Albretch Mueller
<lbrtchx(a)gmail.com> wrote, quoted or indirectly quoted someone who
said :

> How do you deal with these subtle differences without a hack? Is
>there a way to instruct BigInteger to keep leading 0's? Or anyother
>way to do that?

One way is to add 100000...00 then prune the lead 1 off the string
when you are done. Another is to pad the output to fixed length with
lead 0s. See http://mindprod.com/products1.html#COMMON StringTools.
--
Roedy Green Canadian Mind Products
http://mindprod.com
If you think it�s expensive to hire a professional to do the job, wait until you hire an amateur.
~ Red Adair (born: 1915-06-18 died: 2004-08-07 at age: 89)
From: Mike Schilling on
markspace wrote:
> Albretch Mueller wrote:
>>> Left padding. It's not a hack.
>> ~
>> I just thought BigInteger or some Base16 method in java which does
>> not assume you are converting a number, would do
>
>
> I wouldn't use either left padding or DecimalFormat. What happens
> when you get a negative number in BigInteger?

You don't. The constructor the OP used says "this is a positive
number".


From: Real Gagnon on
Lew <noone(a)lewscanon.com> wrote in news:hh8m2c$etj$1(a)news.albasani.net:

> static final String HEXES = "0123456789ABCDEF";
> public static String getHex( byte [] raw )
> {
> if ( raw == null ) // or could just let throw NPE
> {
> return null;
> }
> final StringBuilder hex = new StringBuilder( 2 * raw.length );
> for ( final byte b : raw )
> {
> hex.append( HEXES.charAt( (b & 0xF0) >> 4 )
> .append( HEXES.charAt( (b & 0x0F) );
> }
> return hex.toString();
> }
>

Your suggestion about the usage of a StringBuilder to get rid of the
exception declaration is a good one but your code has a 2 missing ")".

hex.append(HEXES.charAt((b & 0xF0) >> 4))
.append(HEXES.charAt((b & 0x0F)));

Bye.
--
Real Gagnon from Quebec, Canada
* Java, Javascript, VBScript and PowerBuilder code snippets
* http://www.rgagnon.com/howto.html
* http://www.rgagnon.com/bigindex.html