string 怎么转换成byte 数组 java

2025-04-24 06:54:21
推荐回答(1个)
回答1:

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;
}