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

/**
 * <pre>// w ww.ja v  a2  s . c o  m
 * New content schema:
 *
 * [0 - pos) + content + [pos, src.elementLength)
 * </pre>
 *
 * @param src
 *         source array
 * @param pos
 *         start position for content insertion
 * @param content
 *         content which will be inserted from {@param anchor}
 * @return new content
 */
public static byte[] insertInto(byte[] src, int pos, String content) {
    final byte[] contentSrc = content.getBytes(UTF_8);
    final byte[] newSrc = new byte[src.length + contentSrc.length];
    arraycopy(src, 0, newSrc, 0, pos);
    arraycopy(contentSrc, 0, newSrc, pos, contentSrc.length);
    arraycopy(src, pos, newSrc, pos + contentSrc.length, src.length - pos);
    return newSrc;
}

From source file:Main.java

public static String convertToUTF8(String s) {
    String out = null;/*from   w  w  w.  j av a2  s .com*/
    try {
        out = new String(s.getBytes("UTF-8"), "ISO-8859-1");
    } catch (java.io.UnsupportedEncodingException e) {
        return null;
    }
    return out;
}

From source file:Main.java

private static InputStream String2Inputstream(String str) throws UnsupportedEncodingException {
    return new ByteArrayInputStream(str.getBytes("utf-8"));
}

From source file:Main.java

public static byte[] toUtf8Bytes(String data) {
    try {//w w w .  j a v a2  s .c o  m
        return data == null ? null : data.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new IllegalStateException(e);
    }
}

From source file:Main.java

/**
 * <pre>//from  w ww  . j  av  a  2  s .  c  om
 * New content schema:
 *
 * [0 - left) + content + (right, src.elementLength)
 * </pre>
 *
 * @param src
 *         source array
 * @param left
 *         left anchor - not included to result
 * @param right
 *         right anchor - not included to result
 * @param content
 *         content which will be inserted between left and right
 * @return new content
 */
public static byte[] insertBetween(byte[] src, int left, int right, String content) {
    final byte[] contentSrc = content.getBytes(UTF_8);
    final byte[] newSrc = new byte[left + src.length - right + contentSrc.length - 1];
    arraycopy(src, 0, newSrc, 0, left);
    arraycopy(contentSrc, 0, newSrc, left, contentSrc.length);
    arraycopy(src, right + 1, newSrc, left + contentSrc.length, src.length - right - 1);
    return newSrc;
}

From source file:Main.java

public static byte[] toAsciiBytes(String data) {
    try {/*from w  ww .j  av  a 2  s. com*/
        return data == null ? null : data.getBytes("US-ASCII");
    } catch (UnsupportedEncodingException e) {
        throw new IllegalStateException(e);
    }
}

From source file:Main.java

public static byte[] stringToByte(String str) {
    byte[] s2byte = null;
    try {/*from  ww  w .java  2  s. co m*/
        s2byte = str.getBytes("GB2312");
    } catch (UnsupportedEncodingException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    return s2byte;
}

From source file:Main.java

public static String GBK2ISO(String str) {
    String ret = null;/*from   w ww  .j  av  a 2  s .co  m*/
    try {
        ret = new String(str.getBytes("GBK"), "ISO-8859-1");
    } catch (Exception e) {
        ret = "GBK2ISO error. str=" + str;
    }
    return (ret);
}

From source file:Main.java

public static String md5(String string) {
    try {//  www . j ava  2s  .co m
        return computeMD5(string.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Huh,UTF-8 should be supported?", e);
    }
}

From source file:Main.java

public static String ISO2UTF8(String str) {
    String ret = null;// ww  w  .  j  av a2  s .co m
    try {
        ret = new String(str.getBytes("ISO-8859-1"), "UTF-8");
    } catch (Exception e) {
        ret = "ISO2UTF8 error. str=" + str;
    }
    return (ret);
}