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

public static String iso2utf8(String iso) {
    try {/*from w  w  w  .  ja  v a  2 s .c om*/
        String utf8 = new String(iso.getBytes("iso8859-1"), "utf-8");
        return utf8;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return iso;
}

From source file:Main.java

public static String base64Encode(String str) {
    try {//from  www .  ja  v  a2s .c o  m
        str = Base64.encodeToString(str.getBytes("UTF-8"), Base64.DEFAULT);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return str;
}

From source file:Main.java

private static final byte[] stringToBytesUTF8(final String str) {
    try {//from   w  w w  .j av a  2 s  .c o  m
        return str.getBytes(UTF8.name());
    } catch (final UnsupportedEncodingException x) {
        throw new RuntimeException(x);
    }
}

From source file:Main.java

public static final byte[] getGB2312ByteArray(String str) {
    byte[] tmp;//from  w w  w.  j ava2s .c  o  m
    try {
        tmp = str.getBytes("GB2312");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }
    return tmp;

}

From source file:Main.java

/**
 * @param message/* ww  w  . j  a va2s .  c o  m*/
 * @return byte[]
 * @throws UnsupportedEncodingException 
 */
private static byte[] getByteArray(final String message) throws UnsupportedEncodingException {
    return message.getBytes(UTF_8_ENCODING);
}

From source file:Main.java

public static boolean isGB2312(char c) {
    Character ch = Character.valueOf(c);
    String sCh = ch.toString();
    try {/*from w w w .jav a  2 s  . com*/
        byte[] bb = sCh.getBytes("gb2312");
        if (bb.length > 1) {
            return true;
        }
    } catch (java.io.UnsupportedEncodingException ex) {
        return false;
    }
    return false;
}

From source file:Main.java

public static int stringSize(String s) {
    try {//from w ww  .  j  a  v  a 2s. co  m
        String anotherString = new String(s.getBytes("GBK"), "ISO8859_1");
        return anotherString.length();
    } catch (UnsupportedEncodingException ex) {
        ex.printStackTrace();
        return 0;
    }
}

From source file:Main.java

/**
 * Encodes a string with Base64//from ww w.  j a v  a  2  s. c o m
 *
 * @param input String to be encoded
 * @return Base64 encoded input
 * @throws UnsupportedEncodingException
 */
public static String encodeBase64(String input) throws UnsupportedEncodingException {
    byte[] data = input.getBytes("UTF-8");
    return Base64.encodeToString(data, Base64.DEFAULT);
}

From source file:Main.java

public static long doChecksum(String text) throws UnsupportedEncodingException {
    byte bytes[] = text.getBytes("UTF-8"); // This makes this hash function platform independent
    Checksum checksum = new CRC32();
    checksum.update(bytes, 0, bytes.length);
    long lngChecksum = checksum.getValue();
    return lngChecksum;
}

From source file:Main.java

public static String convertBase64(String str) {
    byte[] data;// ww w .ja  v  a 2 s.c om
    try {
        data = str.getBytes("UTF-8");
        String base64 = Base64.encodeToString(data, Base64.DEFAULT);
        return base64.trim();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return "";
}