get Byte array from string using bit shift - Android java.lang

Android examples for java.lang:Byte Array

Description

get Byte array from string using bit shift

Demo Code



public class Main{

    public static byte[] getBytes(String in) {
        byte[] result = new byte[in.length() * 2];
        int output = 0;
        for (char ch : in.toCharArray()) {
            result[output++] = (byte) (ch & 0xFF);
            result[output++] = (byte) (ch >> 8);
        }//w  w  w .  ja  v  a2s .c om
        return result;
    }

}

Related Tutorials