Tuesday, October 18, 2011

Program to reverse the hexadecimal value of elements in a byte array


public class ReverseHexaofByteArray
{

            protected static final byte[] Hexhars =
            {
                        '0', '1', '2', '3', '4', '5',
                        '6', '7', '8', '9', 'a', 'b',
                        'c', 'd', 'e', 'f'
            };

            public static String byteArrayToHexString(byte[] b)
            {                       
StringBuilder st = new StringBuilder(2 * b.length);

                        for (int j = b.length - 1; j > -1; j --)
                        {
                                    int w = b[j] & 0xff;
                                    st.append((char) Hexhars[w >> 4]);
                                    st.append((char) Hexhars[w & 0xf]);
                                    System.out.println(st.toString());
                        }
                        return st.toString();
            }

            public static void main(String[] args)
            {
                        byte[] oBs =
                        {
                                    65, 55, 88
                        };
                        String strResult = byteArrayToHexString(oBs);
                        System.out.println(strResult);
            }
}

No comments:

Post a Comment

Thanks for visiting my Blog....Ur comments n suggestions are most valuable for me...Thanks