Java String to Byte Array convertStringToByteArray(String input)

Here you can find the source of convertStringToByteArray(String input)

Description

Helper-method to emulate the getByte() method missing from the JRE emulation library.

License

Apache License

Declaration

protected static byte[] convertStringToByteArray(String input) 

Method Source Code

//package com.java2s;
/*// w  w w .  j  a  v a  2  s.c om
 * Copyright 2008 Adrian Buerki
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

public class Main {
    /**
     * Helper-method to emulate the <code>getByte()</code> method
     * missing from the JRE emulation library.
     */
    protected static byte[] convertStringToByteArray(String input) {
        char[] chars = input.toCharArray();
        byte[] bytes1 = new byte[chars.length * 2];

        int i = 0;

        for (char c : chars) {
            if (c < 128) {
                bytes1[i] = (byte) c;
                i++;
            } else {
                bytes1[i] = (byte) -61;
                i++;

                bytes1[i] = (byte) ((int) c - 320);
                i++;
            }
        }

        byte[] bytes2 = new byte[i];

        for (int j = 0; j < i; j++) {
            bytes2[j] = bytes1[j];
        }

        return bytes2;
    }
}

Related

  1. asBytes(String basicString)
  2. asBytes(String hex)
  3. asBytes(String hexStr)
  4. convertStringToByte(String input)
  5. convertStringToByte(String strValue)
  6. convertStringToByteArray(String string)
  7. convertStringToBytes(String string)
  8. getBytes(String k)
  9. getBytes(String k)