base64 byte[] 16进制 string 各种转换

来源:互联网 发布:structure软件 k值 编辑:程序博客网 时间:2024/05/18 02:19
文件转换为16进制******************************************************************************************/*文件16进制编码*/public static String hexFile(MultipartFile  replayFile) throws IOException{  char[] strChar = IOUtils.toCharArray(replayFile.getInputStream());     String resultBinary = "";       for (int i = 0; i < strChar.length; i++) {     resultBinary += Integer.toHexString(strChar[i]);     } ************************************可以继续BASE64编码转换**************************************************/*两种方式BASE64编码转换*/   /*A  Sun internal*///sun.misc.BASE64Encoder en = new sun.misc.BASE64Encoder();  //String base64File = en.encode(resultBinary.getBytes());/*B  apache ...*/ //String base64File=org.apache.commons.codec.binary.Base64.encodeBase64String(resultBinary.getBytes());  return resultBinary; }base64转换为byte[]*********************************************************************BASE64Decoder decoder = new BASE64Decoder();   byte[] baseToByte = null;  try {   baseToByte =decoder.decodeBuffer(reply);  } catch (IOException e) {   // TODO Auto-generated catch block   e.printStackTrace();  }base64转换为16 string 类型*********************************************************************public String getReply() throws IOException {  BASE64Decoder decoder = new BASE64Decoder();   byte[] baseToByte = null;  try {   baseToByte =decoder.decodeBuffer(reply);  } catch (IOException e) {   // TODO Auto-generated catch block   e.printStackTrace();  }//  String replyString = new String(baseToByte);//  String replyString=IOUtils.toString(baseToByte, "UTF-8");      StringBuilder stringBuilder = new StringBuilder("");      for (int i = 0; i < baseToByte.length; i++) {            int v = baseToByte[i] & 0xFF;            String hv = Integer.toHexString(v);            if (hv.length() < 2) {                stringBuilder.append(0);            }            stringBuilder.append(hv);        }    return stringBuilder.toString(); }

0 0
原创粉丝点击