zoukankan      html  css  js  c++  java
  • 附件上传byte2hex二行制转字符串优化方法

     public static String byte2hex_(byte[] b) {
      String hs = "";
      String stmp = "";
      int len = b.length;
      for (int n = 0; n < len; n++) {
       stmp = Integer.toHexString(b[n] & 0xFF);
       if (stmp.length() == 1)
        hs = hs + "0" + stmp;
       else
        hs = hs + stmp;
      }
      return hs;
     }

     public static String byte2hex(byte[] b) {
      StringBuffer hs = new StringBuffer(b.length);
      String stmp = "";
      int len = b.length;
      for (int n = 0; n < len; n++) {
       stmp = Integer.toHexString(b[n] & 0xFF);
       if (stmp.length() == 1)
        hs = hs.append("0").append(stmp);
       else {
        hs = hs.append(stmp);
       }
      }
      return String.valueOf(hs);
     }

    上面byte2hex_和byte2hex两个方法比较:

    byte2hex的效率和传输量明显优化于byte2hex_,因为String 操作后都是产生一个新的字符串对象,而stringBuffer操作的始终是原对象, 当字符串长度大时,并且多字要进行字符串连接时,使用 StringBuffer 性能要高许多。 而且 StringBuffer 是线程同步的。

    ######################附原始类代码###########################

    package *.*.mss.util;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.PrintStream;

    public class ByteStringUtils {

     public static String byte2hex_(byte[] b) {
      String hs = "";
      String stmp = "";
      int len = b.length;
      for (int n = 0; n < len; n++) {
       stmp = Integer.toHexString(b[n] & 0xFF);
       if (stmp.length() == 1)
        hs = hs + "0" + stmp;
       else
        hs = hs + stmp;
      }
      return hs;
     }

     public static String byte2hex(byte[] b) {
      StringBuffer hs = new StringBuffer(b.length);
      String stmp = "";
      int len = b.length;
      for (int n = 0; n < len; n++) {
       stmp = Integer.toHexString(b[n] & 0xFF);
       if (stmp.length() == 1)
        hs = hs.append("0").append(stmp);
       else {
        hs = hs.append(stmp);
       }
      }
      return String.valueOf(hs);
     }
     
     public static byte[] hex2byte(String str) {
      if (str == null) {
       return null;
      }
      str = str.trim();
      int len = str.length();
      if ((len == 0) || (len % 2 == 1))
       return null;
      byte[] b = new byte[len / 2];
      try {
       for (int i = 0; i < str.length(); i += 2) {
        b[(i / 2)] = (byte) Integer.decode(
          "0x" + str.substring(i, i + 2)).intValue();
       }
       return b;
      } catch (Exception e) {
      }
      return null;
     }

     public static void main(String[] args) {
      String str = "absadfawegsdcd";

      String result = "";

      result = byte2hex(str.getBytes());

      System.out.println(result);

      System.out.println(new String(hex2byte(result)));

      File imgFile = new File("f:\\temp\\csmc-553.bmp");
      try {
       FileInputStream fis = new FileInputStream(imgFile);
       byte[] bytes = (byte[]) null;
       try {
        bytes = new byte[fis.available()];
        fis.read(bytes);
        fis.close();
       } catch (IOException e) {
        e.printStackTrace();
       }

       String aaa = byte2hex(bytes);

       FileOutputStream output = new FileOutputStream(
         "f:\\temp\\Vista.png");

       byte[] b = hex2byte(aaa);
       try {
        output.write(b);
        output.flush();
        output.close();
       } catch (IOException e) {
        e.printStackTrace();
       }

      } catch (FileNotFoundException e) {
       e.printStackTrace();
      }
     }
    }

  • 相关阅读:
    我的技术移民之路(一)移,还是不移
    【新消息】 请问签证和护照有什么区别啊?_爱问知识人
    Australia Visa Information China Chinese Visa Types
    HqBaiduMusic百度音乐高品质下载Chrome扩展
    分享:一个支持并发, 支持异步/同步, 支持http/https, 支持续传的avhttp库
    linux世界里类似source insight的工具(zz)如梦初醒中国教育人博客
    分享:Fix8 0.7.0 发布,C++ 金融信息交换协议实现
    我的技术移民之路(一)移,还是不移
    转:javascript null和undefined 区别
    javascript Frame和IFrame
  • 原文地址:https://www.cnblogs.com/anuoruibo/p/2737691.html
Copyright © 2011-2022 走看看