
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Characters in a String as an Array of Bytes in Java
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
Advertisements