Skip to content
Related Articles

Related Articles

Improve Article

Java Program to Convert Byte Array to Hex String

  • Last Updated : 07 Jun, 2021

Given a string the task is to convert that string into Integer array.

Illustration:

Input : string : "1 2 3 4 5 6 7 8 9 0"
output : Integer array : [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

Input : string : {"123", "345", "437", "894"}
output : Integer array : [123, 345, 437, 894]

Input : string : "[1,2,356,678,3378]"
output : Integer array : [1, 2, 356, 678, 3378]

Methods:

  1. Using Integer.parseInt() method
  2. Using Integer.valueOf() method
  3. Using string.replaceAll() method
  4. Using string.split() method

Method 1: Using Integer.parseInt() method

This is an in-build method in java.lang package. Integer.parseInt() method is used to parse the string argument to a signed decimal Integer object. The characters in the string must be an integer value. 



Example:

Java




// Java Program to Convert Byte Array to Hex String
// Using Integer.parseInt() method
 
// Importing required classes
import java.io.*;
import java.util.Arrays;
 
// Main class
public class GFG {
    // Main driver method
    public static void main(String args[])
    {
        // string declaration
        String[] str = { "123", "345", "437", "894" };
        int size = str.length;
 
        // array declaration with string size
        int[] arr = new int[size];
 
        // printing the string
        System.out.println("String : " + str);
 
        // parsing the String argument as a signed decimal
        // integer object and storing that integer into the
        // array
        for (int i = 0; i < size; i++) {
            arr[i] = Integer.parseInt(str[i]);
        }
 
        // priting the integer array
        System.out.println("Integer array : "
                           + Arrays.toString(arr));
    }
}

 
 

Output
String : [Ljava.lang.String;@2f4d3709
Integer array : [123, 345, 437, 894]

 

Method 2: Using Integer.valueOf() method

 

This is an in-build method in lang package. Integer.valueOf() method is used to return the integer object. The characters in the string must be an integer value. 

 



Example

 

Java




// Java Program to Convert Byte Array to Hex String
// Using Integer.valueOf() method
 
// Importing required classes
import java.io.*;
import java.util.Arrays;
public class GFG {
    public static void main(String[] args)
    {
        // string declaration
        String[] str = { "123", "345", "437", "894" };
        int size = str.length;
 
        // declaring an array with the size of string
        int[] arr = new int[size];
 
        // printing the string
        System.out.print("String : " + str);
 
        // parsing the String argument as a signed decimal
        // integer object and storing that integer into the
        // array
        for (int i = 0; i < size; i++) {
            arr[i] = Integer.valueOf(str[i]);
        }
 
        // printing the Integer array
        System.out.println("Integer array : "
                           + Arrays.toString(arr));
    }
}

 
 

Output
String : [Ljava.lang.String;@2f4d3709Integer array : [123, 345, 437, 894]

 

Method 3: Using string.replaceAll() method  

 

The replaceAll() method of the String class takes two arguments, one is regex and the replacement values. This method will replace the given regex with the given replacement value and after that, the split() method is used to split the string.

 

Example



 

Java




// Java Program to Convert Byte Array to Hex String
// Using string.replaceAll() method
 
// Importing required classes
import java.io.*;
import java.util.Arrays;
 
public class GFG {
    public static void main(String[] args)
    {
        // declaring the string
        String str = "[1,2,356,678,3378]";
 
        // calling replaceAll() method and split() method on
        // string
        String[] string = str.replaceAll("\\[", "")
                              .replaceAll("]", "")
                              .split(",");
 
        // declaring an array with the size of string
        int[] arr = new int[string.length];
 
        // parsing the String argument as a signed decimal
        // integer object and storing that integer into the
        // array
        for (int i = 0; i < string.length; i++) {
            arr[i] = Integer.valueOf(string[i]);
        }
 
        // printing string
        System.out.print("String : " + str);
 
        // printing the Integer array
        System.out.print("\nInteger array : "
                         + Arrays.toString(arr));
    }
}

 
 

Output
String : [1,2,356,678,3378]
Integer array : [1, 2, 356, 678, 3378]

 

Method 4: Using string.split() method

 

string.split() method is used to split the string into various substrings. Then those substrings are then converted to integer by using Integer.parseInt() method and store that value integer value to the Integer array.

 

Example

 



Java




// Java Program to Convert Byte Array to Hex String
// Using string.split() method
 
// Importing required classes
import java.io.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class GFG {
    // Function for conversion
    pubblic static int[] method(String str)
    {
        int i;
 
        String[] splitArray = str.split(" ");
        int[] array = new int[splitArray.length];
 
        // parsing the String argument as a signed decimal
        // integer object and storing that integer into the
        // array
        for (i = 0; i < splitArray.length; i++) {
            array[i] = Integer.parseInt(splitArray[i]);
        }
        return array;
    }
 
    // main method
    public static void main(String[] args)
    {
        // Bufferedreader declaration
        BufferedReader br = new BufferedReader(
            new InputStreamReader(System.in));
 
        // string declaration
        String str = "1 23 456 7890";
        int i;
 
        // declaring the variable & calling the method with
        // passing string as an argument
        int[] array = method(str);
 
        // printing the string
        System.out.print("\nString : " + str);
 
        // printing the Integer array
        System.out.println("Integer array : ");
        for (i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
    }
}

 

 

Output: 

 

string = "1 23 456 7890"
Int array = [ 1 23 456 7890 ]

 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :