原文:http://www.x-beta.cn/programme/Java-hex-string-convert-7730.html

          package com.nantian.iclient.atm.sdb;

          public class Util {
          public Util() {
          }

          /**
          * 將指定byte數組以16進制的形式打印到控制臺
          * @param hint String
          * @param b byte[]
          * @return void
          */
          public static void printHexString(String hint, byte[] b) {
          System.out.print(hint);
          for (int i = 0; i < b.length; i++) {
          String hex = Integer.toHexString(b[i] & 0xFF);
          if (hex.length() == 1) {
          hex = '0' + hex;
          }
          System.out.print(hex.toUpperCase() + " ");
          }
          System.out.println("");
          }

          /**
          *
          * @param b byte[]
          * @return String
          */
          public static String Bytes2HexString(byte[] b) {
          String ret = "";
          for (int i = 0; i < b.length; i++) {
          String hex = Integer.toHexString(b[i] & 0xFF);
          if (hex.length() == 1) {
          hex = '0' + hex;
          }
          ret += hex.toUpperCase();
          }
          return ret;
          }

          /**
          * 將兩個ASCII字符合成一個字節;
          * 如:"EF"--> 0xEF
          * @param src0 byte
          * @param src1 byte
          * @return byte
          */
          public 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;
          }

          /**
          * 將指定字符串src,以每兩個字符分割轉換為16進制形式
          * 如:"2B44EFD9" --> byte[]{0x2B, 0x44, 0xEF, 0xD9}
          * @param src String
          * @return byte[]
          */
          public static byte[] HexString2Bytes(String src){
          byte[] ret = new byte[8];
          byte[] tmp = src.getBytes();
          for(int i=0; i<8; i++){
          ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]);
          }
          return ret;
          }

          }

          posted on 2008-05-12 14:39 湘江夜游神 閱讀(389) 評論(0)  編輯  收藏 所屬分類: JAVA生活

          Locations of visitors to this page

          主站蜘蛛池模板: 景泰县| 舒兰市| 文山县| 乳山市| 桐城市| 靖远县| 枣庄市| 保康县| 蓝田县| 开远市| 辽阳县| 法库县| 施秉县| 湖南省| 肇庆市| 同德县| 安塞县| 靖远县| 来凤县| 自贡市| 昭平县| 灵璧县| 大丰市| 车致| 遂溪县| 九江县| 石柱| 灵璧县| 和静县| 太和县| 苏尼特左旗| 平阴县| 镇原县| 公安县| 子长县| 广河县| 陈巴尔虎旗| 广安市| 化隆| 莆田市| 夏邑县|