Friday, April 8, 2011

how to convert hex to byte for the following program?

public static String asHex (byte buf[]) {
    StringBuffer strbuf = new StringBuffer(buf.length * 2);
    int i;

    for (i = 0; i < buf.length; i++) {
        if (((int) buf[i] & 0xff) < 0x10)
            strbuf.append("0");

        strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
    }

    return strbuf.toString();
}
From stackoverflow
  • To get a hexadecimal string representation of a byte array, you could use String.format() with the %X format specifier:

    public static String asHex(byte buf[]) 
    {
        StringBuffer strbuf = new StringBuffer(buf.length * 2);
    
        for (byte b : buf)
           strbuf.append(String.format("%02X", b));
    
        return strbuf.toString();
    }
    

    The following method gives the inverse operation, returning a byte array representation of a hexadecimal string. It uses Byte.parseByte() and some bit shifting to get two characters in one byte:

    public static byte[] asBytes(String str)
    {
        if ((str.length() % 2) == 1) str = "0" + str; // pad leading 0 if needed
        byte[] buf = new byte[str.length() / 2];
        int i = 0;
    
        for (char c : str.toCharArray())
        {
            byte b = Byte.parseByte(String.valueOf(c), 16);
            buf[i/2] |= (b << (((i % 2) == 0) ? 4 : 0));
            i++;
        }
    
        return buf;
    }
    
    Zach Scrivena : @silvia: Updated answer to include the inverse operation.
    paxdiablo : I have two questions: (1) When did Java introduce that for-statement syntax (it's nifty but my Eclipse flags error but it has 1.4)? (2) Why the hideous :-) bitshift, can't you just process two chars at a time?
    Zach Scrivena : @Pax: (1) for-each appeared in Java 1.5 http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html (2) Doesn't seem to work unfortunately; Byte.parseByte("FF",16) throws a NumberFormatException.
    Zach Scrivena : @Pax: Integer.parseInt() works nicely of course =)
  • Here's a complete program which includes the asBytes() function which is what I assume you were looking for, the opposite to asHex():

    public class Temp {
        public static String asHex (byte buf[]) {
            StringBuffer strbuf = new StringBuffer(buf.length * 2);
            int i;
            for (i = 0; i < buf.length; i++) {
                if (((int) buf[i] & 0xff) < 0x10)
                    strbuf.append("0");
                strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
            }
           return strbuf.toString();
        }
        public static byte[] asBytes (String s) {
            String s2;
            byte[] b = new byte[s.length() / 2];
            int i;
            for (i = 0; i < s.length() / 2; i++) {
                s2 = s.substring(i * 2, i * 2 + 2);
                b[i] = (byte)(Integer.parseInt(s2, 16) & 0xff);
            }
            return b;
        }
        static public void main(String args[]) {
            byte[] b = Temp.asBytes("010203040506070809fdfeff");
            String s = Temp.asHex(b);
            System.out.println (s);
        }
    }
    
  • can anyone explain me the program which is written as

    public static String asHex (byte buf[]) { StringBuffer strbuf = new StringBuffer(buf.length * 2); int i;

    for (i = 0; i < buf.length; i++) {
        if (((int) buf[i] & 0xff) < 0x10)
            strbuf.append("0");
    
        strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
    }
    
    return strbuf.toString();
    

    }

  • Check here for couple of good methods to do this.

  • i need the explanation of the above code.urgent.please

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.