In order to convert a String to byte[] array in Java make use of the .getBytes() method from the String class,
Example:public class JavaStringToByteArray {
public static void main(String[] args) {
String string = "Hello World!";
byte[] byteArr = string.getBytes();
for(int i = 1;i <byteArr.length; i++) {
System.out.print(byteArr[i]+" ");
}
}
}
Output:
101 108 108 111 32 87 111 114 108 100 33 %
To convert byte[] array to String: https://code2care.org/java/convert-byte-arrry-to-string-in-java
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
More Posts:
- How to Adjust macOS System Font Size - MacOS
- MySQL: Error Code: 1175 You are using safe update mode - MySQL
- How to Restart Apple Silicon based Mac (M1/M2 Chip) in Safe Mode - MacOS
- Delete blank lines in a file using Notepad++ - NotepadPlusPlus
- Google Local Guide Program and Perks of Contributing to Google Maps - Google
- Program 6: Find Sum of Two Floating Numbers - 1000+ Python Programs - Python-Programs
- How to add days to a date in Python - Python
- How to delete all text after a character or string in Notepad++ - NotepadPlusPlus