- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to get the Characters in a String as an Array of Bytes
To get the characters in a string as an array of bytes, use the getBytes() method.
Let’s say we have the following string.
String str = "Demo Text!";
Convert it to array of bytes.
byte[] arr = str.getBytes();
Example
public class Demo { public static void main(String[] args) { String str = "Demo Text!"; // getting as an array of bytes byte[] arr = str.getBytes(); // displaying each character as a Byte value for(byte res: arr) { System.out.println(res); } } }
Output
68 101 109 111 32 84 101 120 116 33
- Related Articles
- Extracting a substring as an array of characters in Java
- Swift Program to convert the string into an array of characters
- Java Program to get day of week as string
- Return the array data as a string containing the raw bytes in the array in Numpy
- C# program to count number of bytes in an array
- Convert bytes to a string in java
- How to get the length of a string in bytes in JavaScript?
- Java Program to Find the Duplicate Characters in a String
- Java Program to Add Characters to a String
- Java Program to fill an array of characters from user input
- 8085 program to add two consecutive bytes of an array
- 8085 program to subtract two consecutive bytes of an array
- Java Program to find duplicate characters in a String?
- Java program to check order of characters in string
- Golang Program to get characters from a string using the index

Advertisements