Java Byte Array from getBytes(String data, String encoding)

Here you can find the source of getBytes(String data, String encoding)

Description

Convert a string to an array of bytes, representing the string in the given encoding.

License

Open Source License

Parameter

Parameter Description
data a parameter
encoding a parameter

Declaration

public static byte[] getBytes(String data, String encoding) 

Method Source Code


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

public class Main {
    /**//www.  ja  va  2 s.c  o m
     * Convert a string to an array of bytes, representing the string in the
     * given encoding. This works like {@link String#getBytes(String)}, but if
     * the encoding is unknown, the resulting {@Llink UnsupportedEncodingException}
     * is wrapped in a {@link RuntimeException}.
     *
     * @param data
     * @param encoding
     * @return
     */
    public static byte[] getBytes(String data, String encoding) {
        try {
            return data.getBytes(encoding);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. getBytes(final String data, String charset)
  2. getBytes(final String value, final String characterEncoding)
  3. getBytes(int length, InputStream inStream)
  4. getBytes(int value)
  5. getBytes(String content)
  6. getBytes(String defaultPlain)
  7. getBytes(String fileName)
  8. getBytes(String fileName)
  9. getBytes(String filePath)