Showing posts with label bytearray. Show all posts
Showing posts with label bytearray. Show all posts

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

Friday, January 28, 2011

Java program to insert image as byte array to postgres database

//Program to insert an image into postgres database as byte array

import java.sql.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class insertImage
{
  public static void main(String[] args)
  {
    PreparedStatement statement = null;
    {
      FileInputStream fin = null;
      try
      {
        System.out.println("Insert Image Example!");
        Class.forName("org.postgresql.Driver");
        String url = "jdbc:postgresql://x.x.x.x:5432/MYDB";
        Connection oConnection = DriverManager.getConnection(url, "username", "password");
        System.out.println("Sucessfully connected to Postgres Database");
        File imgfile = new File("myimage.jpg");
        fin = new FileInputStream(imgfile);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        for (int readNum; (readNum = fin.read(buf)) != -1;)
        {
          bos.write(buf, 0, readNum);
          byte[] bytes = bos.toByteArray();
          String sql = "INSERT INTO my_table (byte_array) VALUES (?)";
          statement = oConnection.prepareStatement(sql);
          statement.setBytes(1, bytes);
          statement.executeUpdate();
          System.out.println("Image inserted into database!");

        }
        statement.close();
        oConnection.close();
      }
      catch (Exception ex)
      {
        System.out.println("Exception:- " + ex);
      }
      try
      {
        fin.close();
      }
      catch (IOException ex)
      {
        System.out.println(" IOException:- " + ex);
      }
    }
  }
}