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 void main(String[] args) throws UnsupportedEncodingException {
    String s = "Hello world";
    byte[] b = s.getBytes("US-ASCII");
    System.out.println(Arrays.toString(b));
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String s = "0123456789";
    byte ptext[] = s.getBytes("UTF8");
    for (int i = 0; i < ptext.length; i++) {
        System.out.print(ptext[i] + ",");
    }/*  w w w .j a v  a 2s . c o  m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String pound = "\u00a3";
    byte[] bytes = pound.getBytes(Charset.forName("UTF-8"));
    for (byte b : bytes) {
        System.out.println(b & 0xff); // 194, 163
    }//from w  ww .j a  v a 2  s. c  om
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String string = "abc\u5639";
    byte[] utf8 = string.getBytes("UTF-8");

    string = new String(utf8, "UTF-8");
    System.out.println(string);//from  www  .j  a  v  a 2  s .  c o m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    String string = "abc\u5639\u563b";
    byte[] utf8 = string.getBytes("UTF-8");

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String os = "abc";
    for (byte b : os.getBytes("UTF-16BE")) {
        System.out.println(b);//from   w w  w .  j a  v a  2s.  c  o  m
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    for (char c : os.toCharArray()) {
        dos.writeChar(c);
    }

    byte[] ba = baos.toByteArray();

    for (byte b : ba) {
        System.out.println(b);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5");
    SecretKey key = keyGen.generateKey();

    Mac mac = Mac.getInstance(key.getAlgorithm());
    mac.init(key);/*from  w  w  w .j a va 2  s. c o m*/

    String str = "This message will be digested";

    byte[] utf8 = str.getBytes("UTF8");
    byte[] digest = mac.doFinal(utf8);

    String digestB64 = new sun.misc.BASE64Encoder().encode(digest);
    System.out.println(digestB64);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String text = "Converting String to InputStream Example";
    InputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
}

From source file:Main.java

public static void main(String[] args) throws Exception { // main method
    // Encode a String into bytes
    String inputString = "this is a test";
    byte[] input = inputString.getBytes("UTF-8");

    // Compress the bytes
    byte[] output1 = new byte[input.length];
    Deflater compresser = new Deflater();
    compresser.setInput(input);/*from www.j  a v  a2 s . com*/
    compresser.finish();
    int compressedDataLength = compresser.deflate(output1);
    compresser.end();

    String str = new String(Base64.getEncoder().encode(output1));
    System.out.println("Deflated String:" + str);

    byte[] output2 = Base64.getDecoder().decode(str);

    // Decompress the bytes
    Inflater decompresser = new Inflater();
    decompresser.setInput(output2);
    byte[] result = str.getBytes();
    int resultLength = decompresser.inflate(result);
    decompresser.end();

    // Decode the bytes into a String
    String outputString = new String(result, 0, resultLength, "UTF-8");
    System.out.println("Deflated String:" + outputString);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String input = "Hello world!";
    byte[] uncompressedData = input.getBytes("UTF-8");

    byte[] compressedData = compress(uncompressedData, Deflater.BEST_COMPRESSION, false);

    byte[] decompressedData = decompress(compressedData, false);

    String output = new String(decompressedData, "UTF-8");

    System.out.println("Uncompressed data length: " + uncompressedData.length);
    System.out.println("Compressed data length:  " + compressedData.length);
    System.out.println("Decompressed data length:  " + decompressedData.length);
}