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

  • 相关阅读:
    shell的随机数
    centos7 安装install_mysql5.7网络教程安装_无报错.sh
    关于所有运动框架总结
    仅一年工作经验成功跳槽字节跳动,腾讯并拿到字节的offer,全靠这份面经!
    面试必看!花了三天整理出来的并发编程的锁及内存模型,看完你就明白了!
    新鲜出炉!花了三天整理的JVM复习知识点,面试突击必备!
    深度分析!面试99%被问到的多线程和并发篇,看完你就懂了
    去年去阿里面试,被问到ArrayList和LinkedList,我是这样回答的!
    深度分析:面试阿里,字节99%会被问到Java类加载机制和类加载器
    深度分析:Java并发编程之线程池技术,看完面试这个再也不慌了!
  • 原文地址:https://www.cnblogs.com/anuoruibo/p/2737691.html
Copyright © 2011-2022 走看看