Java Byte Array Create toByteArray(String string)

Here you can find the source of toByteArray(String string)

Description

Convert the passed in String to a byte array by taking the bottom 8 bits of each character it contains.

License

Apache License

Parameter

Parameter Description
string the string to be converted

Return

a byte array representation

Declaration

public static byte[] toByteArray(String string) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from w  w w . java 2  s . com
     * Convert the passed in String to a byte array by taking the bottom 8 bits
     * of each character it contains.
     * 
     * @param string
     *            the string to be converted
     * @return a byte array representation
     */
    public static byte[] toByteArray(String string) {
        byte[] bytes = new byte[string.length()];
        char[] chars = string.toCharArray();

        for (int i = 0; i != chars.length; i++) {
            bytes[i] = (byte) chars[i];
        }

        return bytes;
    }
}

Related

  1. toByteArray(String str)
  2. toByteArray(String str)
  3. toByteArray(String str)
  4. toByteArray(String str, String separator)
  5. toByteArray(String string)
  6. toByteArray(String strTransInfo)
  7. toByteArray(String uid)
  8. toByteArray(String value)
  9. toByteArray(String[] anArray)