附件上传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();
  }
 }
}

 

转载于:https://www.cnblogs.com/anuoruibo/archive/2012/10/24/2737691.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值