Java UTF8 Convert To getBytesUtf8(String string)

Here you can find the source of getBytesUtf8(String string)

Description

Encodes the given string into a sequence of bytes using the UTF-8 charset, storing the result into a new byte array.

License

Open Source License

Parameter

Parameter Description
string the String to encode, may be null

Return

encoded bytes, or null if the input string was null

Declaration

public static byte[] getBytesUtf8(String string) 

Method Source Code


//package com.java2s;
import java.io.UnsupportedEncodingException;

public class Main {
    /**//ww w  .  j a v  a2s . co  m
     * Encodes the given string into a sequence of bytes using the UTF-8 charset, storing the result into a new byte
     * array.
     *
     * @param string
     *            the String to encode, may be {@code null}
     * @return encoded bytes, or {@code null} if the input string was {@code null}
     */
    public static byte[] getBytesUtf8(String string) {
        try {
            return string == null ? null : string.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            // Should never happen with UTF-8
            // If it does - ignore & return null
        }
        return null;
    }
}

Related

  1. fromUTF8Bytes(byte[] bytes)
  2. fromUTF8Bytes(byte[] bytes)
  3. getBytesUtf8(String str)
  4. getBytesUTF8(String string)
  5. getBytesUtf8(String string)
  6. getBytesUtf8(String string)
  7. UTF8ToCodePoint(byte[] b, int s)
  8. utf8ToCodePoint(int b1, int b2, int b3, int b4)
  9. utf8Togb2312(String str)