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 ; }
Convert a string to a byte array no encoding is used . String must only contain characters <256 .