🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Working with various data types is a common task in Java programming, and sometimes you may need to convert data from one type to another. A typical scenario is converting a byte array to a char array. This can be necessary for various reasons, such as when dealing with file I/O, network communications, or encoding conversions. In this blog post, we'll explore how to convert a byte array to a char array in Java.
Understanding Byte and Char in Java
Before diving into the conversion process, it's important to understand the difference between byte and char data types in Java:
Byte: The byte data type in Java is an 8-bit signed two's complement integer. It's primarily used for raw data manipulation.
Char: The char data type in Java is a single 16-bit Unicode character. It's used to represent characters.
Conversion Method 1
Using Standard Charset Java's Charset class can be used to convert a byte array to a char array by first creating a String from the byte array and then converting this string to a char array.
Example:
import java.nio.charset.StandardCharsets;
public class ByteArrayToCharArray {
public static void main(String[] args) {
byte[] byteArray = { 72, 101, 108, 108, 111 }; // "Hello" in ASCII
String string = new String(byteArray, StandardCharsets.UTF_8);
char[] charArray = string.toCharArray();
System.out.println(charArray);
}
}
Output:
Hello
Method 2: Using Character Encoding
Example:
import java.nio.charset.Charset;
public class ByteArrayToCharArray {
public static void main(String[] args) {
byte[] byteArray = { 72, 101, 108, 108, 111 }; // "Hello" in ASCII
Charset encoding = Charset.forName("UTF-8");
String string = new String(byteArray, encoding);
char[] charArray = string.toCharArray();
System.out.println(charArray);
}
}
Output:
Hello
Method 3: Manual Conversion
Example:
public class ByteArrayToCharArray {
public static void main(String[] args) {
byte[] byteArray = { 72, 101, 108, 108, 111 }; // "Hello" in ASCII
char[] charArray = new char[byteArray.length];
for (int i = 0; i < byteArray.length; i++) {
charArray[i] = (char) byteArray[i];
}
System.out.println(charArray);
}
}
Output:
Hello
Conclusion
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment