Wednesday, December 24, 2008

Byte value and Hex String value converter.

This java coding is a simple one which anyone can use to try the previously posted encryption and decryption program. The Convertor class has two methods. First one is convertHexToByte () method and the second one is convertByteToHex () method.

                This is not a standard type of a method, but it has a special algorithm for conversion. I know that anybody can convert a Hex String by using java.lang.String.getBytes () method to return an array of byte values, but it has the same length of the String. But that type of a method will not be useful with encryption / decryption processes. So we can use my method for that purpose.

                The convertByteToHex () method is also has a special algorithm. It will return good results when using with the convertHexToByte () method. And you can add a decimalization table to the result if you want so.

/**

 * @author Sashika Nimantha Perera

 */

public class Convertor {

    public static byte[] convertHexToByte(String sHexString)

                    {

                        if(sHexString == null) return null;

                        sHexString = sHexString.trim().toUpperCase();

                        if((sHexString.length() % 2) > 0) return null;

                        int iLengthInBytes = sHexString.length()/2;

                        byte[] abMessage = new byte[iLengthInBytes];

 

                        for (int i = 0; i <>

                          {

                            String sOneByte = sHexString.substring(i*2, i*2 + 2);

                            Integer I = Integer.valueOf(sOneByte, 16);

                            byte b = I.byteValue();

                            abMessage[i] = b;

                          }

                        System.out.println(" convertHexToByte: length in bytes: " + abMessage.length);

                        return abMessage;

        }

 

 

    public  static String convertByteToHex(byte [] bByteArray){

                                                String sResult="";

 

                                for(int i=0; i

                          {

                             sResult = sResult + byteConvert(Integer.toHexString(bByteArray[i]));

                             //System.out.println(sResult);

              }

 

                                                return sResult;

                                }

 

                private static String byteConvert(String x){

                                                                if (x.length() == 1)

                                                                   {

                                                                       x = "0" + x;

                                                                   }

                                                                else if (x.length() == 8)

                                                                   {

                                                                       x = x.substring((x.length()-2), x.length());

                                                                   }

                                                                //            Decimalization Table

                                                                    //x = x.replace('a','1');

                                                                    //x = x.replace('b','2');

                                                                    //x = x.replace('c','3');

                                                                    //x = x.replace('d','4');

                                                                    //x = x.replace('e','5');

                                                                    //x = x.replace('f','6');

                                                // remove the comments if the result want a decimalization table support.

                                                                return x;

                                                                                }

 

}

3 comments:

  1. Nice post.

    codes can be scary when looked at but when you work with them they become friendly.

    smile*

    ReplyDelete
  2. i posted this in a hurry, so there was no time to align the codes to a reader friendly mode. But as you say, when you copy and paste them in another text editor there will be no problem

    ReplyDelete