[Java] How to convert a character string from String type to byte type

Programming study diary

December 28, 2020 Today I learned how to convert a string to byte type in Java, so I will summarize it.

What is byte type?

One of the primitive types, which stores values ​​up to 8 bits. Since 1 bit is 0 or 1, 1 byte can represent a bit array from 00000000 to 11111111. Since the byte type is signed and takes a negative value, it takes a value from -128 to 127. Since image files, audio files, and character strings are also a collection of bytes internally, they are often used as a collection of binary data as a byte array rather than handling byte type variables alone.

How to convert from String type to byte type

Use the getBytes () method. The getBytes () method converts a string into a set of bytes and returns it as a byte array.

Basic writing


Sample code

public class Sample {
  public static void main(String[] args) throws InterruptedException, UnsupportedEncodingException {

    String str = "The string you want to convert";
    byte[] def = str.getBytes();
    byte[] utf8 = str.getBytes("UTF-8");
    byte[] utf16 = str.getBytes("UTF-16");




Execution result

[-27, -92, -119, -26, -113, -101, -29, -127, -105, -29, -127, -97, -29, -127, -124, -26, -106, -121, -27, -83, -105, -27, -120, -105]
[-27, -92, -119, -26, -113, -101, -29, -127, -105, -29, -127, -97, -29, -127, -124, -26, -106, -121, -27, -83, -105, -27, -120, -105]
[-2, -1, 89, 9, 99, -37, 48, 87, 48, 95, 48, 68, 101, -121, 91, 87, 82, 23]


Active engineers explain how to convert a character string from String type to byte type in Java [For beginners]

Recommended Posts

[Java] How to convert a character string from String type to byte type
[Java] How to erase a specific character from a character string
[Android] How to convert a character string to resourceId
[Convenient to remember !!!] How to convert from LocalDate type to character string and from character string to LocalDate type
[Java] How to convert from String to Path type and get the path
[Java] How to cut out a character string character by character
How to convert a file to a byte array in Java
[Java] How to convert one element of a String type array to an Int type
Convert a Java byte array to a string in hexadecimal notation
How to store a string from ArrayList to String in Java (Personal)
[Java] How to use substring to cut out a character string
I tried to convert a string to a LocalDate type in Java
[Java] Convert Object type null to String type
Type conversion from java BigDecimal type to String type
Convert from C String pointer to Swift String type
How to convert a solidity contract to a Java contract class
[Java] How to use substring to cut out a part of a character string
Convert String type to Timestamp type
How to convert Java radix
How to jump from Eclipse Java to a SQL file
java: How to write a generic type list [Note]
How to convert A to a and a to A using AND and OR in Java
[Java] I want to convert a byte array to a hexadecimal number
[Java] Incompatible types error occurred when assigning a character string to a char type variable
How to write a migration from Rails datetime type to date type
How to make a Java container
[Java] How to create a folder
Convert iso-2022-jp character string to utf-8
Change only one character from the String type string to uppercase
How to make a Java array
How to use Java enum type
A memorandum when I investigated how to convert from Epoch Time to Date type in Scala
How to check for the contents of a java fixed-length string
[Java] Divide a character string by a specified character
How to make a Java calendar Summary
[Rails] How to convert from erb to haml
Convert from java UTC time to JST time
[Introduction to Java] How to write a Java program
How to make a Discord bot (Java)
How to output Java string to console screen
Java: How to send values from Servlet to Servlet
How to print a Java Word document
[Java] Comparison of String type character strings
About the method to convert a character string to an integer / decimal number (cast data) in Java
[Java] How to get to the front of a specific string using the String class
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
Convert to a tag to URL string in Rails
[java] Summary of how to handle character strings
Connect to Aurora (MySQL) from a Java application
How to get a heapdump from a Docker container
How to display a web page in Java
Code to escape a JSON string in Java
Convert a string to a character-by-character array with swift
How to get Class from Element in Java
[Java] How to switch from open jdk to oracle jdk
[Ruby] How to generate a random alphabet string
How to write Java String # getBytes in Kotlin?
[Java] Difference between equals and == in a character string that is a reference type
[Spring Boot] How to get properties dynamically from a string contained in a URL
[Java] How to easily get the longest character string of ArrayList using stream
How to ZIP a JAVA CSV file and manage it in a Byte array