Prev: Properties.list(System.err) not listing strings?
Next: One Of The Easiest Ways To Promote Your Site!
From: Albretch Mueller on 27 Dec 2009 16:50 // __ http://www.rgagnon.com/javadetails/java-0596.html The simple way public static String getHexString(byte[] b) throws Exception { String result = ""; for (int i=0; i < b.length; i++) { result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring ( 1 ); } return result; } A faster way import java.io.UnsupportedEncodingException; public class StringUtils { static final byte[] HEX_CHAR_TABLE = { (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f' }; public static String getHexString(byte[] raw) throws UnsupportedEncodingException { byte[] hex = new byte[2 * raw.length]; int index = 0; for (byte b : raw) { int v = b & 0xFF; hex[index++] = HEX_CHAR_TABLE[v >>> 4]; hex[index++] = HEX_CHAR_TABLE[v & 0xF]; } return new String(hex, "ASCII"); } public static void main(String args[]) throws Exception{ byte[] byteArray = { (byte)255, (byte)254, (byte)253, (byte)252, (byte)251, (byte)250 }; System.out.println(StringUtils.getHexString(byteArray)); /* * output : * fffefdfcfbfa */ } }
From: Lew on 27 Dec 2009 17:11 Albretch Mueller wrote: > // __ http://www.rgagnon.com/javadetails/java-0596.html > > The simple way > public static String getHexString(byte[] b) throws Exception { > String result = ""; > for (int i=0; i < b.length; i++) { > result += > Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring > ( 1 ); > } > return result; > } > > A faster way > import java.io.UnsupportedEncodingException; > > public class StringUtils { > > static final byte[] HEX_CHAR_TABLE = { > (byte)'0', (byte)'1', (byte)'2', (byte)'3', > (byte)'4', (byte)'5', (byte)'6', (byte)'7', > (byte)'8', (byte)'9', (byte)'a', (byte)'b', > (byte)'c', (byte)'d', (byte)'e', (byte)'f' > }; > > public static String getHexString(byte[] raw) > throws UnsupportedEncodingException > { > byte[] hex = new byte[2 * raw.length]; > int index = 0; > > for (byte b : raw) { > int v = b & 0xFF; > hex[index++] = HEX_CHAR_TABLE[v >>> 4]; > hex[index++] = HEX_CHAR_TABLE[v & 0xF]; > } > return new String(hex, "ASCII"); > } // ... > } You can avoid encoding and some obfuscation by making the table 'char []' and using 'StringBuilder hex' instead of a 'byte[]', otherwise the same algorithm. Or the table can be "0123456789ABCDEF" and the StringBuilder can append charAt()s. No more specious exception, either. 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(); } -- Lew
From: Lew on 29 Dec 2009 17:46 Lew wrote: >> 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(); >> } > Real Gagnon wrote: > 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))); > Oopsie. That's the risk of posting code one hasn't compiled. When I do occasionally compile code before posting, I enclose it in the (fake XML) tag "<sscce>" or explicitly mention that I compiled the code first, otherwise typos and bugs are left for the reader to discover. Good catch. -- Lew
First
|
Prev
|
Pages: 1 2 3 Prev: Properties.list(System.err) not listing strings? Next: One Of The Easiest Ways To Promote Your Site! |