Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!border3.nntp.ams.giganews.com!Xl.tags.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!local2.nntp.ams.giganews.com!nntp.bt.com!news.bt.com.POSTED!not-for-mail NNTP-Posting-Date: Thu, 20 Jun 2013 13:22:16 -0500 Date: Thu, 20 Jun 2013 19:22:13 +0100 From: lipska the kat <"nospam at neversurrender dot co dot uk"> Organization: Trollbusters 3 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130308 Thunderbird/17.0.4 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: convert byte array to hex string using BigInteger References: <9hv5s85cul11v42sfop0388joag6mu6t4e@4ax.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Message-ID: Lines: 121 X-Usenet-Provider: http://www.giganews.com X-AuthenticatedUsername: NoAuthUser X-Trace: sv3-F4zxjECBgZ+ObFtoJ0PwymjvIyvwI8PX8VMAWRiWuIPFVPAtXPn20uTKu/cSv2Sg9VkKIE4vAn+uGWl!2hmSi/rPV2nFge7cRJKWE0JiZVfFE5v0sLLjkXV1VRMKbgiFrXChBeNj78eAxb7bQaZsAfBPNPo= X-Complaints-To: abuse@btinternet.com X-DMCA-Complaints-To: abuse@btinternet.com X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 4534 Xref: csiph.com comp.lang.java.programmer:24488 On 20/06/13 14:32, Laura Schmidt wrote: > On 06/20/2013 03:09 PM, rossum wrote: > >> This is a case of RTFM. The Javadocs for BigInteger(byte[]) tell you >> that it expects the byte array in two's complement representation, so >> a leading 1 bit is interpreted as a negative number. You need a >> different constructor: >> >> public BigInteger(int signum, byte[] magnitude) > > You are right, sorry. > > Now I get a hex string without sign. But now the decode method does not > return the original bytes anymore: > > private byte [] hex_decode (String val) > { > BigInteger b = new BigInteger (val,16); > byte [] t = b.toByteArray(); > > return (t); > } > > There is no other constructor for byte arrays. > And I don't really understand why it doesn't return the original byte > array. I'm not sure what all this twos compliment stuff is about twos compliment is just a number representation scheme There should be no need to 'convert' anything Anyway The following program works with the following observation If the most significant byte is positive e.g >= 1 && <= 127 or 0x7F the conversion works both ways If the most significant byte is negative or 0 e.g <= 0 && >= -128 or 0x80 then the conversion works with one proviso The contract for BigInteger#toByteArray() contains the following text "The array will contain the minimum number of bytes required to represent this BigInteger, including at least one sign bit" so, if the MSB is negative there will be an additional byte in the MSG position after calling toByteArray set to 0 which indicates that the following is a positive number, if the MSB is positive there is no need for an additional byte as the first bit in the MSB is 0 thereby marking the following number as positive ... interesting, never seen this before... apart from that it seems to work. Try changing the byte variable a to a positive number to see the difference. import java.math.BigInteger; public class Converter { private static void printBytes(byte[] val){ for(byte b : val){ System.out.print(b); System.out.print(' '); } System.out.print("\n"); } private static String hexEncode (byte[] val){ BigInteger b = new BigInteger(1, val); String t = b.toString(16); return t; //Just Say No to this :-) //return new BigInteger(1, val).toString(16); } private static byte[] hexDecode (String val){ BigInteger b = new BigInteger (val, 16); byte[] t = b.toByteArray(); return t; } public static void main(String[] args) { byte a = -1; //0xFF byte b = 127; //0x7F byte c = -128; //0x80 byte d = 64; //0x40 byte e = 16; //0x10 byte f = 99; //0x5A byte[] val = {a, b, c, d, e, f}; printBytes(val); String encoded = hexEncode(val); System.out.println(encoded); byte[] decoded = hexDecode(encoded); printBytes(decoded); encoded = hexEncode(decoded); System.out.println(encoded); } } lipska -- Lipska the KatŠ: Troll hunter, sandbox destroyer and farscape dreamer of Aeryn Sun