Java String to Byte Array getBytes(String str)

Here you can find the source of getBytes(String str)

Description

Should always be able convert to/from UTF-8, so encoding exceptions are converted to an Error to avoid adding throws declarations in lots of methods.

License

Open Source License

Parameter

Parameter Description
str String

Return

utf8 bytes

Declaration

public static byte[] getBytes(String str) 

Method Source Code

//package com.java2s;

public class Main {
    /**//  www .  j  a  v  a2 s  . c  o  m
     * Should always be able convert to/from UTF-8, so encoding exceptions are converted to an Error to avoid adding
     * throws declarations in lots of methods.
     * @param str String
     * @return utf8 bytes
     * @see String#getBytes()
     */
    public static byte[] getBytes(String str) {
        try {
            return str.getBytes("UTF8");
        } catch (java.io.UnsupportedEncodingException e) {
            throw new Error("String to UTF-8 conversion failed: "
                    + e.getMessage());
        }
    }
}

Related

  1. getBytes(String s)
  2. getBytes(String s, String charsetName)
  3. getBytes(String s, String encoding)
  4. getBytes(String s, String encoding)
  5. getBytes(String str)
  6. getBytes(String str)
  7. getBytes(String str)
  8. getBytes(String str)
  9. getBytes(String str)