How to Convert a String to Specific Character Encoding in Java? Last Updated : 29 Jan, 2024 Comments Improve Suggest changes Like Article Like Report In Java, the process of converting a String to a certain character encoding is converting the string's character sequence to a byte array and applying the chosen character encoding. Java Program to Convert a String to a Specific Character EncodingUsing the String class's getBytes() function to transform a string into a byte array is the fundamental idea. The Charset class also allows you to define the character encoding. An overloaded version of the getBytes() function accepts a Charset as a parameter. Syntax:public byte[] getBytes()Below is the implementation to Convert a String to a specific character encoding in Java: Java // Java Program to Convert a String // Specific Character Encoding import java.nio.charset.Charset; // Driver Class public class StringToEncodingExample { // Main Function public static void main(String[] args) { // Create a sample string String originalString = "Hello, Rahul!"; // Convert the string to bytes using a specific encoding byte[] utf8Bytes = originalString.getBytes(Charset.forName("UTF-8")); // Display the original string and the encoded bytes System.out.println("Original String: " + originalString); System.out.println("UTF-8 Encoded Bytes: " + bytesToHex(utf8Bytes)); } // Method to convert byte array to hexadecimal string private static String bytesToHex(byte[] bytes) { StringBuilder hexStringBuilder = new StringBuilder(); for (byte b : bytes) { hexStringBuilder.append(String.format("%01X ", b)); } return hexStringBuilder.toString(); } } OutputOriginal String: Hello, Rahul! UTF-8 Encoded Bytes: 48 65 6C 6C 6F 2C 20 52 61 68 75 6C 21 Explaination of the above Program:The Program is divided it in different section: Input String is declaredConverting String to Byte array storingConverting byte to hexadecimal string and then printing.Printing the Final Result. Comment More infoAdvertise with us Next Article How to Convert a String to Specific Character Encoding in Java? R rahul709392 Follow Improve Article Tags : Java Java Programs Java-Strings Java-Charset Java Examples +1 More Practice Tags : JavaJava-Strings Similar Reads How to Convert a String to a Character in Java? String and char are fundamental and most commonly used datatypes in Java. A String is not a primitive data type like char. To convert a String to char in Java, we have to perform character-based operations or have to process individual characters.In this article, we will learn how to convert a Strin 3 min read How to Convert Char to String in Java? In this article, we will learn how to Convert Char to String in Java. If we have a char value like 'G' and we want to convert it into an equivalent String like "G" then we can do this by using any of the following three listed methods in Java. Using toString() method of Character classUsing valueOf( 5 min read Convert a String to Character Array in Java Converting a string to a character array is a common operation in Java. We convert string to char array in Java mostly for string manipulation, iteration, or other processing of characters. In this article, we will learn various ways to convert a string into a character array in Java with examples.E 2 min read Convert String to Byte Array in Java Using getBytes(encoding) Method In Java, any sequence of characters within double quotes is treated as String literal. String class represents the character string literal. The string class is present in java.lang package. All string literals in java are immutable i.e their value cannot be changed once created. In Java, the string 3 min read How to Create a File with a Specific charset in Java? In Java, when we write text to a file, we must provide the character encoding to create a file with a specified charset. Classes to generate Java Files with Specified CharsetUsing a FileOutputStream and the OutputStreamWriter class we can generate a Java file with a specified charset. FileOutputStre 2 min read How to Convert String to Char Stream in Java without Using Library Functions? In Java, we can convert a string to a char stream with a simple built-in method .toCharArray(). Here, we want to convert the string to a char stream without using built-in library functions. So, we will create an empty char array iterate it with string characters, and store the individual characters 2 min read Convert String to Stream of Chars in Java The StringReader class from the java.io package in Java can be used to convert a String to a character stream. When you need to read characters from a string as though it were an input stream, the StringReader class can be helpful in creating a character stream from a string. In this article, we wil 2 min read Convert a String to a ByteBuffer in Java In Java, ByteBuffer can be used to perform operations at the Byte level one more thing is this class provides different types of methods for reading writing, and manipulating bytes in a structured way only. In this article, we will learn about String to ByteBuffer in Java. Java Program to Convert St 4 min read Java Program to Convert Char to Int Given a char value, and our task is to convert it into an int value in Java. We can convert a Character to its equivalent Integer in different ways, which are covered in this article.Examples of Conversion from Char to Int:Input : ch = '3'Output : 3Input : ch = '9'Output : 9The char data type is a s 4 min read How to Check if a String contains only ASCII in Java? The full form of ASCII is the American Standard Code for Information Interchange. It is the numeric representation of Character. As Java supports multiple languages, and it follows the Unicode system. In simple terms for better understanding, it converts the Character to a specific unique number, an 3 min read Like