TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java Convert String to Byte Array

Convert byte arrays into Strings and Strings into byte arrays. Use the String constructor and the getBytes method.
String, byte array. Strings are composed of characters, and characters of bytes. Chars use 2 bytes each, but often with ASCII text they can be represented in 1 byte.Strings
With the String constructor, we create a String based on the elements in a byte array. And with String's getBytes methods, we convert String data to byte arrays.Constructors
Byte array to String. This program creates a byte array with the values 97, 98 and 99. These numbers are ASCII codes for the lowercase letters "abc."

Tip: Variants of the String constructor exist. One constructs based on a range of bytes (specified by a start and offset).

Charset: A Charset is an optional argument. If your String contains a specific encoding of chars, please use a Charset.

Java program that uses String constructor public class Program { public static void main(String[] args) { // These are the ASCII values for lowercase A, B and C. byte[] array = { 97, 98, 99 }; String result = new String(array); System.out.println(result); } } Output abc
Convert from String to byte array. Excellent support too exists for converting a String to a byte array. We use getBytes, a method on the String class.

No argument: With no argument, we use the system's default charset to convert the String to a byte array.

String argument: Here a Charset is used based on the name provided. We test this with US-ASCII which supports 7-bit chars.

Arrays.toString: The example uses Array.toString to "pretty-print" the arrays to the Console output.

Java program that uses getBytes import java.io.UnsupportedEncodingException; import java.util.Arrays; public class Program { public static void main(String[] args) throws UnsupportedEncodingException { // The string we want to convert. String letters = "abc"; System.out.println(letters); // Use default charset. byte[] valuesDefault = letters.getBytes(); // ... Use Arrays.toString to display our byte array. System.out.println(Arrays.toString(valuesDefault)); // Specify US-ASCII char set directly. byte[] valuesAscii = letters.getBytes("US-ASCII"); System.out.println(Arrays.toString(valuesAscii)); } } Output abc [97, 98, 99] [97, 98, 99]
Charsets are complex. Not all text is easy-to-process 7-bit ASCII. With byte arrays, we convert 2 byte chars into 1 byte elements. This is problematic but sometimes safe.
In Java, built-in approaches, like the String constructor and the getBytes methods, are handy. We can specify charsets. And for debugging, Arrays.toString displays byte arrays.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf