Convert a string to a byte array, no encoding is used. String must only contain characters <256.
527
public static byte[] str2bytes(String str) throws IOException {
		byte[] b=new byte[str.length()];
		for(int i=0; i<str.length(); ++i) {
			char c=str.charAt(i);	
			if(c>255) throw new UnsupportedEncodingException("string contained a char > 255, cannot convert to bytes");
			b[i]=(byte)c;
		}
		return b;
	}
507773
public static byte[] fromBase64(String base64String) {

        byte[] result = null;
        if (base64String != null) {
            result = Base64.decodeBase64(base64String);
        }
        return result;
    }
791431
public static byte[] toUtf8(String str) {
        int i, max;
        char c;
        int len;
        byte[] result;

        max = str.length();
        len = 0;
        for (i = 0; i < max; i++) {
            c = str.charAt(i);
            if ((c >= 0x0001) && (c <= 0x007f)) {
                len += 1;
            } else {
                if ((c == 0) || ((c >= 0x0080) && (c <= 0x07ff))) {
                    len += 2;
                } else {
                    len += 3;
                }
            }
        }
        if (len > 0xffff) {
            throw new IllegalArgumentException("string too long: " + str.length() + "/" + len);
        }
        result = new byte[1 + 2 + len];
        result[0] = CONSTANT_UTF8;
        result[1] = (byte) (len >> 8);
        result[2] = (byte) len;
        len = 3;  // used for current position
        for (i = 0; i < max; i++) {
            c = str.charAt(i);
            if ((c >= 0x0001) && (c <= 0x007f)) {
                result[len] = (byte) c;
                len += 1;
            } else {
                if ((c == 0) || ((c >= 0x0080) && (c <= 0x07ff))) {
                    result[len + 0] = (byte) ((c >> 6) | 0xc0);
                    result[len + 1] = (byte) ((c & 0x3f) | 0x80);
                    len += 2;
                } else {
                    result[len + 0] = (byte) ((c >> 12) | 0xe0);
                    result[len + 1] = (byte) (((c >> 6) & 0x3f) | 0x80);
                    result[len + 2] = (byte) ((c & 0x3f) | 0x80);
                    len += 3;
                }
            }
        }
        if (len != result.length) {
            throw new IllegalStateException(len + " vs " + result.length);
        }
        return result;
    }
1359311
public static byte[] decode( String s )
    {   
        byte[] bytes = s.getBytes();
        return decode( bytes, 0, bytes.length );
    }
694099
function stringToBytes(str) {
  var bytes = new Array();
  var len, c;
  len = str.length;
  for (var i = 0; i < len; i++) {
    c = str.charCodeAt(i);
    if (c >= 0x010000 && c <= 0x10FFFF) {
      bytes.push(((c >> 18) & 0x07) | 0xF0);
      bytes.push(((c >> 12) & 0x3F) | 0x80);
      bytes.push(((c >> 6) & 0x3F) | 0x80);
      bytes.push((c & 0x3F) | 0x80);
    } else if (c >= 0x000800 && c <= 0x00FFFF) {
      bytes.push(((c >> 12) & 0x0F) | 0xE0);
      bytes.push(((c >> 6) & 0x3F) | 0x80);
      bytes.push((c & 0x3F) | 0x80);
    } else if (c >= 0x000080 && c <= 0x0007FF) {
      bytes.push(((c >> 6) & 0x1F) | 0xC0);
      bytes.push((c & 0x3F) | 0x80);
    } else {
      bytes.push(c & 0xFF);
    }
  }
  return bytes;

}
97611
public String encode(String str) throws IOException {
    	byte[] array;
    	try { 
    		array = str.getBytes(encoding);
    	} catch (IOException e) {
    		array = str.getBytes(); // take default
    	}
    	// Calculate expected size to avoid any buffer expansion copies within the ByteArrayOutput code
    	ByteArrayOutputStream baos = new ByteArrayOutputStream((int)(array.length*1.363)); // account for 4 bytes for 3 and a byte or two more
    	
    	encode(new ByteArrayInputStream(array),baos);
    	return baos.toString(encoding);
    }
389154
@Nonnull
  @ReturnsMutableCopy
  public static byte [] decode (@Nonnull final String s) throws IOException
  {
    return decode (s, NO_OPTIONS);
  }
128742
public static byte[] getBytes(String text) {
		byte[] bytes = new byte[] {};
		try {
			bytes = text.getBytes("utf-8");
		} catch (UnsupportedEncodingException e) {
		}
		return bytes;
	}
145804
function (str) {
                str = stringify(str);
                var byteArray = [];

                for (var i = 0, l = str.length, ch; i < l; i++) {
                    ch = str.charCodeAt(i);
                    byteArray.push(ch >> 8);
                    byteArray.push(ch & 0xFF);
                }

                return byteArray;
            }
306774
private static byte[] convert_to_bytes(String string) {
        if (null == string) {
            return null;
        }
        if (0 == string.length()) {
            return EMPTY_BYTE_ARRAY;
        }
        try {
            return string.getBytes(STRING_CONVERSION_CODE);
        } catch (UnsupportedEncodingException e) {
            logger.logp(Level.SEVERE, PasswordUtil.class.getName(), "convert_to_bytes", "PASSWORDUTIL_UNSUPPORTEDENCODING_EXCEPTION", e);
            return null;
        }
    }
1249556
public static ByteBuffer bytes(String s)
    {
        return ByteBuffer.wrap(s.getBytes(StandardCharsets.UTF_8));
    }
331708
public static byte[] fromHexString(String s) {
		if (s.length() % 2 != 0) {
			throw new IllegalArgumentException(s);
		}
		byte[] array = new byte[s.length() / 2];
		for (int i = 0; i < array.length; i++) {
			int b = Integer.parseInt(s.substring(i * 2, i * 2 + 2), SIXTEEN);
			array[i] = (byte) (_0XFF & b);
		}
		return array;
	}
304810
public static byte[] utf8Encode(CharSequence string) {
		try {
			ByteBuffer bytes = UTF8.newEncoder().encode(CharBuffer.wrap(string));
			byte[] bytesCopy = new byte[bytes.limit()];
			System.arraycopy(bytes.array(), 0, bytesCopy, 0, bytes.limit());
			return bytesCopy;
		}
		catch (CharacterCodingException e) {
			throw new RuntimeException(e);
		}
	}
590088
private static byte[] convert_viewable_to_bytes(String string) {
        if (null == string) {
            return null;
        }
        if (0 == string.length()) {
            return EMPTY_BYTE_ARRAY;
        }
        return Base64Coder.base64Decode(convert_to_bytes(string));
    }
803464
public static byte[] toByteArray(String string) {
    EnsureUtil.ensureActiveCommandContext("StringUtil.toByteArray");
    ProcessEngineConfigurationImpl processEngineConfiguration = Context.getProcessEngineConfiguration();
    return toByteArray(string, processEngineConfiguration.getProcessEngine());
  }
1057674
def from_bytes(self, string):
        """"""
        msg = srsly.msgpack_loads(gzip.decompress(string))
        self.attrs = msg["attrs"]
        self.strings = set(msg["strings"])
        lengths = numpy.fromstring(msg["lengths"], dtype="int32")
        flat_spaces = numpy.fromstring(msg["spaces"], dtype=bool)
        flat_tokens = numpy.fromstring(msg["tokens"], dtype="uint64")
        shape = (flat_tokens.size // len(self.attrs), len(self.attrs))
        flat_tokens = flat_tokens.reshape(shape)
        flat_spaces = flat_spaces.reshape((flat_spaces.size, 1))
        self.tokens = NumpyOps().unflatten(flat_tokens, lengths)
        self.spaces = NumpyOps().unflatten(flat_spaces, lengths)
        for tokens in self.tokens:
            assert len(tokens.shape) == 2, tokens.shape
        return self
910543
public static byte[] stringToSalt(final String string) {
    try {
      final MessageDigest digest = MessageDigest.getInstance("SHA-1");
      digest.reset();
      return digest.digest(string.getBytes("UTF-8"));
    } catch (final NoSuchAlgorithmException ex) {
      throw new RuntimeException(ex);
    } catch (final UnsupportedEncodingException ex) {
      throw new RuntimeException(ex);
    }
  }