Example usage for java.lang String getBytes

List of usage examples for java.lang String getBytes

Introduction

In this page you can find the example usage for java.lang String getBytes.

Prototype

public byte[] getBytes(Charset charset) 

Source Link

Document

Encodes this String into a sequence of bytes using the given java.nio.charset.Charset charset , storing the result into a new byte array.

Usage

From source file:Main.java

/**
 * Convert string to byte array, the chaset is UTF-8.
 *
 *//*from w  w w.j a  va 2 s . c  o  m*/
public static byte[] toByteArray(String s) {
    return s.getBytes(Charset.forName("UTF-8"));
}

From source file:Main.java

public static byte[] getBytes(String s) {
    try {//w  w  w  . ja v a  2 s.c  o m
        return s.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        return s.getBytes();
    }
}

From source file:Main.java

public static void writeStringToOutputStream(OutputStream out, String str) throws IOException {
    out.write(str.getBytes("UTF-8"));
}

From source file:Main.java

public static byte[] encode8bitUserData(String text) {
    try {/*from  w  w  w . j  a v  a  2s  . c om*/
        return text.getBytes("ISO8859_1");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static byte[] stringToByteArray(String s) {
    try {//from ww w.  j  a  va2s. com
        return s.getBytes(ENCODING);
    } catch (UnsupportedEncodingException ex) {
        return null;
    }
}

From source file:Main.java

public static final String MD5(String str) {
    return DigestUtils.md5Hex(str.getBytes(charset));
}

From source file:Main.java

public static byte[] toBytes(String str) {
    try {/*from w w w. ja  va  2  s  .  co  m*/
        return str.getBytes(DEFAULT_CHARSET);
    } catch (UnsupportedEncodingException e) {
        return str.getBytes();
    }
}

From source file:Main.java

private static int getBytesLength(String msg) {
    try {/*from  w  w w .  j a  va2  s  .  c  o  m*/
        return msg.getBytes("GB2312").length;
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return 0;
}

From source file:Main.java

/**
 * Get utf8 byte array.// w  ww .j a v  a  2  s. c om
 *
 * @param str
 *            should be "UTF-8" encoded
 * @return array or null if UnsupportedEncodingException was thrown
 */
public static byte[] getUTF8Bytes(String str) {
    try {
        return str.getBytes("UTF-8");
    } catch (UnsupportedEncodingException ex) {
        return null;
    }
}

From source file:Main.java

/**
 * Returns the passed string as a byte array containing the
 * string in UTF-8 representation.//  w w  w. j  a va2s .c om
 * @param str Java string
 * @return UTF-8 byte array
 */
public static byte[] getUtf8Bytes(String str) {
    try {
        return str.getBytes("UTF-8");
    } catch (UnsupportedEncodingException uee) {
        return str.getBytes();
    }
}