加入收藏 收藏网站 设为首页 设为首页
招生考试网
学历类| 阳光高考 研 究 生 自学考试 成人高考 专 升 本 中考会考 外语类| 四 六 级 职称英语 商务英语 公共英语 日语能力
资格类| 公 务 员 报 关 员 银行从业 司法考试 导 游 证 教师资格 财会类| 会 计 证 经 济 师 会计职称 注册会计 税 务 师
工程类| 一级建造 二级建造 造 价 师 造 价 员 咨 询 师 监 理 师 医学类| 卫生资格 执业医师 执业药师 执业护士 国际护士
计算机| 等级考试 软件水平 应用能力 其它类| 书画等级 美国高考 驾 驶 员 书法等级 少儿英语 报 检 员 单 证 员 出国留学
 招生考试网 - 计算机等级考试 - 考试辅导 - 正文

 
JAVA编程:16进制和字节数组互转
来源:fjzsksw.com 2010-1-6 编辑:zhoupanyu 【字体:小 大】

 

  大括号理的就是一个byte类型的数组,里面是3个byte类型那个的数

  public static String stringToHexString(String strPart) {

  String hexString = "";

  for (int i = 0; i < strPart.length(); i++) {

  int ch = (int) strPart.charAt(i);

  String strHex = Integer.toHexString(ch);

  hexString = hexString + strHex;

  }

  return hexString;

  }

  private static String hexString="0123456789ABCDEF";

  /*

  * 将字符串编码成16进制数字,适用于所有字符(包括中文)

  */

  public static String encode(String str)

  {

  // 根据默认编码获取字节数组

  byte[] bytes=str.getBytes();

  StringBuilder sb=new StringBuilder(bytes.length*2);

  // 将字节数组中每个字节拆解成2位16进制整数

  for(int i=0;i<bytes.length;i++)

  {

  sb.append(hexString.charAt((bytes[i]&0xf0)>>4));

  sb.append(hexString.charAt((bytes[i]&0x0f)>>0));

  }

  return sb.toString();

  }

  /*

  * 将16进制数字解码成字符串,适用于所有字符(包括中文)

  */

  public static String decode(String bytes)

  {

  ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2);

  // 将每2位16进制整数组装成一个字节

  for(int i=0;i<bytes.length();i+=2)

  baos.write((hexString.indexOf(bytes.charAt(i))<<4 |hexString.indexOf(bytes.charAt(i+1))));

  return new String(baos.toByteArray());

  }

  private static byte uniteBytes(byte src0, byte src1) {

  byte _b0 = Byte.decode("0x" + new String(new byte[] {src0})).byteValue();

  _b0 = (byte) (_b0 << 4);

  byte _b1 = Byte.decode("0x" + new String(new byte[] {src1})).byteValue();

  byte ret = (byte) (_b0 | _b1);

  return ret;

  public static byte[] HexString2Bytes(String src)

  {

  byte[] ret = new byte[6];

  byte[] tmp = src.getBytes();

  for(int i=0; i<6; ++i )

  {

  ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]);

  }

  return ret;

  }

  /**

  * Convert byte[] to hex string.这里我们可以将byte转换成int,然后利用Integer.toHexString(int)来转换成16进制字符串。

  * @param src byte[] data

  * @return hex string

  */

  public static String bytesToHexString(byte[] src){

  StringBuilder stringBuilder = new StringBuilder("");

  if (src == null || src.length <= 0) {

  return null;

  }

  for (int i = 0; i < src.length; i++) {

  int v = src[i] & 0xFF;

  String hv = Integer.toHexString(v);

  if (hv.length() < 2) {

  stringBuilder.append(0);

  }

  stringBuilder.append(hv);

  }

  return stringBuilder.toString();

  }

  /**

  * Convert hex string to byte[]

  * @param hexString the hex string

  * @return byte[]

  */

  public static byte[] hexStringToBytes(String hexString) {

  if (hexString == null || hexString.equals("")) {

  return null;

  }

  hexString = hexString.toUpperCase();

  int length = hexString.length() / 2;

  char[] hexChars = hexString.toCharArray();

  byte[] d = new byte[length];

  for (int i = 0; i < length; i++) {

  int pos = i * 2;

  d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));

  }

  return d;

  }

  /**

  * Convert char to byte

  * @param c char

  * @return byte

  */

  private byte charToByte(char c) {

  return (byte) "0123456789ABCDEF".indexOf(c);

  }

 



 
网站版权与免责声明
①由于各方面情况的不断调整与变化,本网所提供的相关信息请以权威部门公布的正式信息为准.
②本网转载的文/图等稿件出于非商业性目的,如转载稿涉及版权等问题,请在两周内来电联系.
最新文章
热门文章

报名考试
全国 | 黑龙江 | 吉林 | 辽宁 | 内蒙古
青海 | 宁夏 | 甘肃 | 新疆 | 陕西
西藏 | 北京 | 天津 | 河北 | 山东
江苏 | 安徽 | 河南 | 上海 | 浙江
福建 | 广东 | 山西 | 湖南 | 湖北
江西 | 广西 | 海南 | 云南 | 贵州
四川 | 重庆
分省高校计算机考试
黑龙江 | 吉林 | 辽宁 | 内蒙古 | 河北
北京 | 天津 | 新疆 | 甘肃 | 宁夏
青海 | 陕西 | 山西 | 河南 | 山东
江苏 | 安徽 | 浙江 | 福建 | 广东
海南 | 广西 | 江西 | 湖北 | 湖南
四川 | 上海 | 重庆 | 贵州 | 云南
西藏
成绩查询
报考指南
试题答案
模拟试题
考试辅导
计算机一级 | 计算机二级 | 计算机三级 | 计算机四级
经验交流
高校计算机