Code: Select all
public static String asHex(byte buf)
StringBuffer strbuf = new StringBuffer(buf.length * 2);
for(int i=0; i< buf.length; i++)
if(((int) buf[i] & 0xff) < 0x10)
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
Reason I ask is that this snippet actually came from the Sun Developers Network as part of an example that contained about half a million billion trillion mistakes/redundencies/unused variables/unused imports/bad form/general uselessness that I corrected in my implementation. A few of the things I corrected were symptoms of a poorly aging Hack who thinks it's really cool to use every operator in the language to do the maximum amount of stuff on one line rather than spreading it out across an if statement or something readable. In an example whose sole purpose is to be read.
Edit: Actually, nevermind. This looks like it checks out just fine. looking at DWIM implementations is always funny. (Why the hell is byte signed? I can't think of a single case where I'd want to use a byte to do anything other than store a number from 0 to 255)