Showing posts with label hexa. Show all posts
Showing posts with label hexa. Show all posts

Wednesday, July 25, 2012

Convert hex value stored in string to byte format

I have a hexa value(Fa) in a string. 
To add up some hexa values, first you need to convert each one of them into byte.
This program is used to convert a hexa value stored in string to byte format

/**
 *
 * Program to convert  hex value stored in string to  byte format
 *
 */


public class HexConversion
{           
            public static void main(String[] args)
            {
                        byte v = 0;
                        String s = "Fa";                       
                        byte[] b = s.trim().getBytes();
                        System.out.println(b[0]);
                        byte A, B;                       
                        A = getHexaValue(b[0]);
                        B = getHexaValue(b[1]);

                        System.out.printf("%X", A);
                        System.out.println();
                        System.out.printf("%X", B);
                        System.out.println();
                        /*
                         * Binary of F = 1111 (4 bits)
                         * Binary of a = 1010 (4 bits)
                         * The binary equivalent of Fa is 11111010
                         * b[0] = 70  = F = 00001111
                         * b[1] = 65 = a = 00001010
                         * To get both bytes in a single byte, left shift first byte to 4 positions
                         * OR it with v
                         * Then OR the second byte with v
                         * You will get the result as FA in a single byte
                         */

                        A = (byte) (A << 4); //left shifts value in A to 4 positions
                        System.out.printf("%X", A);
                        System.out.println();
                        v = (byte) (v | A);
                        System.out.printf("%X", v);
                        System.out.println();
                        v = (byte) (v | B);
                        System.out.printf("%X", v);
                        System.out.println();
            }

            //Returns the hexa value of the corresponding byte.
               //Hexa value is 4 in size

            private static byte getHexaValue(byte b)
            {
                        byte val = 0;
                        switch (b)
                        {
                                    case 65://A
                                    case 97://a           
                                                val = 0x0A;
                                                break;
                                    case 66://B
                                    case 98://b
                                                val = 0x0B;
                                                break;
                                    case 67://C
                                    case 99://c                                               
                                                val = 0x0C;
                                                break;
                                    case 68://D
                                    case 100://d
                                                val = 0x0D;
                                                break;
                                    case 69://E
                                    case 101://e
                                                val = 0x0E;
                                                break;
                                    case 70://F
                                    case 102://f
                                                val = 0x0F;
                                                break;
                                               
                                    case 49://1
                                                val = 0x01;
                                                break;
                                    case 50://2
                                                val = 0x02;
                                                break;
                                    case 51://3
                                                val = 0x03;
                                                break;
                                    case 52://4
                                                val = 0x04;
                                                break;
                                    case 53://5
                                                val = 0x05;
                                                break;
                                    case 54://6
                                                val = 0x06;
                                                break;
                                    case 55://7
                                                val = 0x07;
                                                break;
                                    case 56://8
                                                val = 0x08;
                                                break;
                                    case 57://9
                                                val = 0x09;
                                                break;
                        }
                        return val;
            }
}

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);
            }
}

Thursday, October 13, 2011

Program to convert byte array data to hexadecimal

Program to convert byte array data to hexadecimal

public class ByteArrayDemo

{

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

           
            public String byteArrayToHexString(byte[] b)
            {
//Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.
                        StringBuilder s = new StringBuilder(2 * b.length);
                     
                        for (int i = 0; i < b.length; i ++)
                        {
                                    int v = b[i] & 0xff;
                                    /*
                                     * ANDs b[i] with 0xFF
 * The value of b[i] is 65(Whose binary is 01000001), we AND it with 0xFF(whose Binary is 11111111)
                                     * resulting in the binary 01000001
                                     */

                                    s.append((char) Hexhars[v >> 4]);
                                    /*
                                     * v >> 4 - Right shifts the value of v(01000001) to 4 places
                                     * On 1st right shift, we get 00100000
                                    * On 2nd right shift, we get 00010000
                                    * On 3rd right shift, we get 00001000
                                    * On 4th right shift, we get 00000100
                                    * Which is 4
* Hexhars[4] - Finds the value at the 4th place(starting from 0) from the byte array Hexhars
                                    */
                                   
s.append((char) Hexhars[v & 0xf]);
                                    /*
                                     * ANDs b[i] with 0xF
* The value of b[i] is 65(Whose binary is 01000001), we AND it with 0xF(whose Binary is 00001111)
                                    * resulting in the binary 00000001 - 1
                                    * Hexhars[1] - Finds the value at the 1st place from the byte array Hexhars
                                    */
}
                        return s.toString();
            }
           
         
            public static void main(String[] args)
            {
                        byte[] byteArray = new byte[] {65};
                        ByteArrayDemo obyteArrayDemo = new ByteArrayDemo();
                        String sData = obyteArrayDemo.byteArrayToHexString(byteArray);                        
                        System.out.println(sData);                          
            }
}