java string to hex string code example

Example: String to hexadecimal in java

// String to hexadecimal in java
import java.util.Scanner;
public class StringToHexJava
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a string: ");
      String strInput = sc.next();
      StringBuffer sb = new StringBuffer();
      char[] chArray = strInput.toCharArray();
      for(int a = 0; a < chArray.length; a++)
      {
         String strHexadecimal = Integer.toHexString(chArray[a]);
         sb.append(strHexadecimal);
      }
      String strOutput = sb.toString();
      System.out.println(strOutput);
      sc.close();
   }
}

Tags:

Java Example