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

  • 相关阅读:
    由于客观原因,暂时学习php两天,然后继续学习.net
    【任务】html编辑器在vs2003下实现
    one bug og webMatrix when create a new file
    数据统一接口?
    安全3S
    一个订单管理页面
    【总结】浪费3个月向.net继续前进
    关于在asp.net中类的继承问题
    【心得】create a data table in webMatrix is very easy!
    Java与.NET谁是未来
  • 原文地址:https://www.cnblogs.com/anuoruibo/p/2737691.html
Copyright © 2011-2022 走看看