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 byte[] getByteArrFromString(String str) {
    byte[] arr;//  w  ww  . j  av a  2 s. c  o m
    try {
        arr = str.getBytes("ISO-8859-1");
        return arr;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

/**
 * Should always be able convert to/from UTF-8, so encoding exceptions are converted to an Error to avoid adding
 * throws declarations in lots of methods.
 * @param str String// w ww . j  a  v  a2  s  .  c o  m
 * @return utf8 bytes
 * @see String#getBytes()
 */
public static byte[] getBytes(String str) {
    try {
        return str.getBytes("UTF8");
    } catch (java.io.UnsupportedEncodingException e) {
        throw new Error("String to UTF-8 conversion failed: " + e.getMessage());
    }
}

From source file:Main.java

/**
 * Convert this String into a sequence of bytes using the UTF-8 charset.
 * @param str//from w  w  w . java  2s . c  o  m
 * @return
 */
/* package protected */static byte[] convertUTF16ToUTF8(String str) {
    try {
        return str.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        return str.getBytes();
    }
}

From source file:Main.java

/**
 * Converts a string to a byte array using UTF8 encoding.
 *///  w  ww  .  j a  v  a 2  s. c om
public static byte[] string2Bytes(String str) {
    try {
        return str.getBytes("UTF8");
    } catch (UnsupportedEncodingException e) {
        assert false : "UTF8 encoding is not supported ";
    }
    return null;
}

From source file:Main.java

public static byte[] getBytes(final String s) {
    try {/*from ww w .  j ava2s.com*/
        return s.getBytes("UTF-8");
    } catch (final UnsupportedEncodingException e) {
        return s.getBytes();
    }
}

From source file:Main.java

public static byte[] getBytes(String fromString) {
    try {/*from ww  w  .j  a  v  a 2  s  .c om*/
        return fromString.getBytes("UTF8");
    } catch (UnsupportedEncodingException e) {
        throw new AssertionError(e);
    }
}

From source file:Main.java

static public boolean isCN(String str) {
    try {//from  w  w w.j a va  2s.c  o  m
        byte[] bytes = str.getBytes("UTF-8");
        if (bytes.length == str.length()) {
            return false;
        } else {
            return true;
        }
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static void saveNamedFile(String patch, String data, String name) {

    try {//w ww .  j av a 2 s.  co m

        File direct = new File(patch);
        if (!direct.exists())
            direct.mkdirs();

        File f = new File(patch + "/" + name);
        if (!f.exists())
            f.createNewFile();

        FileOutputStream fos = new FileOutputStream(f);
        String s = data;
        fos.write(s.getBytes("UTF-8"));
        fos.close();

    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }

}

From source file:Main.java

public static byte[] getBytes(String data) {
    try {//from   ww  w .j av a 2 s  .  c  o m
        return data.getBytes("iso-8859-1");
    } catch (UnsupportedEncodingException e) {
    }
    return new byte[0];
}

From source file:Main.java

public static byte[] generateKey(String password) throws Exception {
    byte[] keyStart = password.getBytes("UTF-8");

    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
    sr.setSeed(keyStart);//  ww w .  ja  v a 2  s.  co m
    kgen.init(128, sr);
    SecretKey skey = kgen.generateKey();
    return skey.getEncoded();
}