Java: Convert String to Byte Array

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Java: Multidimensional Arrays

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Strings & Bytes & ASCII
  • 1:15 Converting Strings to…
  • 2:52 Converting Byte Arrays…
  • 3:38 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Getting a string into byte-sized chunks can be a handy way to work with text data in Java. This lesson will define the concept and provide working code examples for the best way to go about doing this.

Strings & Bytes & ASCII

Sometimes we need to break up a long string of text into separate pieces. Perhaps we're testing a login name for a specific value or character, or changing individual items. Remember that a string is really a combination of individual characters, and everything is made up of bytes.

When we convert a string to an array of bytes, we are taking each character from the string and displaying the ASCII value of that byte. ASCII stands for the American Standard Code for Information Interchange. An ASCII value is a number the computer can read and interpret its character value. You can display ASCII values on your own.

Try this: make sure your Num Lock is ON. In a text-editing program, such as Notepad, Word, or even an e-mail message, type the Alt Key and hold it. While pressed, type 72 on your keypad, not the numbers above the letters on the keyboard. The letter H will appear in your text editor. For fun, try Alt+153.

When you break apart each character from the string and put in a byte array, each bucket in the array displays the ASCII value of that character.

Converting Strings to Byte Arrays

Thankfully Java provides us with tools to work with strings and arrays. Java calls these utilities util and they consist of a variety of methods for specific tasks. When you want to use one of the utilities, you can import it at the beginning of your code. For example, we'll be using the Arrays utility in our program. To import this utility, this code is used:


Import utility


This utility includes a method called getBytes, which splits out each individual byte in the string. Let's create a string variable and assign it the value Welcome Home. Finally we'll print out both the original string and the string as it is broken down into bytes.

Okay, now for the meat of the code. We are going to declare an array of bytes and use the getBytes method from the utility to convert the string to an array of bytes, like this:

Java convert string to bye array


This code can be broken down as follows. The byte array, convertEntry, is declared by using the standard array code with two brackets after the data type. Next, we invoke the getBytes() method on the original string, entryText, which does the heavy lifting to break apart the string into individual bytes.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support