- 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
Convert String to UTF-8 bytes in Java
Before converting a String to UTF-8-bytes, let us have a look at UTF-8.
UTF-8 is a variable width character encoding. UTF-8 has ability to be as condense as ASCII but can also contain any unicode characters with some increase in the size of the file. UTF stands for Unicode Transformation Format. The '8' signifies that it allocates 8-bit blocks to denote a character. The number of blocks needed to represent a character varies from 1 to 4.
In order to convert a String into UTF-8, we use the getBytes() method in Java. The getBytes() method encodes a String into a sequence of bytes and returns a byte array.
Declaration − The getBytes() method is declared as follows −
public byte[] getBytes(String charsetName)
where charsetName is the specific charset by which the String is encoded into an array of bytes.
Let us see a program to convert a String into UTF-8 bytes in Java.
Example
public class Example { public static void main(String args[]) throws Exception { String s = "Hello World"; byte arr[] = s.getBytes("UTF8"); for (byte x: arr) { System.out.print(x+" "); } } }
Output
72 101 108 108 111 32 87 111 114 108 100
Let us understand the above program. We have created a String s −
String s = "Hello World";
String s is assigned the value Hello World.
To convert it into UTF-8, we use the getBytes(“UTF-8”) method. This gives us an array of bytes as follows −
byte[] arr = s.getBytes("UTF-8");
Then to print the byte array, we use an enhanced for loop as follows −
for (byte x: arr) { System.out.print(x+" "); }
- Related Articles
- Convert Unicode to UTF-8 in Java
- Convert UTF-8 to Unicode in Java
- Convert bytes to a string in java
- Convert ASCII TO UTF-8 Encoding in PHP?
- How to convert wrongly encoded data to UTF-8 in MySQL?
- How would you convert string to bytes in Python 3?
- How to convert an MySQL database characterset and collation to UTF-8?
- How to convert Date to String in Java 8?
- How can I convert bytes to a Python string?
- How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters in java?
- UTF-8 Validation in C++
- Change MySQL default character set to UTF-8 in my.cnf?
- Convert Java String to Short in Java
- How to read and write unicode (UTF-8) files in Python?
- Program to convert KiloBytes to Bytes and Bits in C++
