string 怎么转换成byte 数组 java

如题所述

public static void main(String[] args){
    String str = "您的回答被采纳后将获得系统奖励";
    byte[] b = getBytes(str);
    if(b!=null){
        //......
    }
}


public byte[] getBytes(String str){
    if(StringUtils.isNotBlank(str)){
        try {
            return str.trim().getBytes("UTF-8");
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            }
      }
    return null;
}

温馨提示:答案为网友推荐,仅供参考
相似回答