is there any function in java that converts string to byte array ?
Yes: String.getBytes
. You really, really want to specify the character encoding when you do so though - using the platform default encoding is almost always the wrong thing to do.
Ideally, it's best to specify the encoding via a Charset
- that way you don't need to worry about the UnsupportedEncodingException
which can be thrown by the overload of getBytes
which just takes a String with the character encoding name as an argument.
EDIT: Based on your comment, it looks like you want to parse a hex string into a byte array. (It would have been useful to say so in your question.) String.getBytes
is inappropriate for this - I don't believe there's anything which does this in the standard libraries, but the Apache Commons Codec library makes it pretty easy:
byte[] data = Hex.decodeHex(text.toCharArray());