首页  »   编程

Java对二进制与字符串变换

网友分享于:2013-12-29  浏览:13次
Java对二进制与字符串转换
package test;

  import java.util.StringTokenizer;

  public class StrToByte {

  public static String byte2hex(byte[] b) // 二进制转字符串

  {

  String hs = "";

  String stmp = "";

  for (int n = 0; n < b.length; n++) {

  stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));

  if (stmp.length() == 1)

  hs = hs + "0" + stmp;

  else

  hs = hs + stmp;

  }

  return 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)));

  }

  }

 

转载:http://java.chinaitlab.com/dynamic/828909.html

 

相关解决方案

最新解决方案