George World

          技術(shù)改變明天,創(chuàng)新塑造未來! i(at)huadev.com Follow Me: http://t.sina.com.cn/georgeworld

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            28 隨筆 :: 0 文章 :: 5 評(píng)論 :: 0 Trackbacks

          /*
          * To change this template, choose Tools | Templates
          * and open the template in the editor.
          */
          package test.util;

          /**
          *
          * @author Administrator
          */
          public class StringUtil {

              public StringUtil() {
              }

              /**
               * 將指定byte數(shù)組以16進(jìn)制的形式打印到控制臺(tái)
               * @param hint String
               * @param b byte[]
               * @return void
               */
              public static String printHexString( byte[] b) {
                  StringBuffer returnValue = new StringBuffer();
                  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() + " ");
                      returnValue.append(hex.toUpperCase() + " ");
                  }

                  return "[" + returnValue.toString() + "]";
              }

              /**
               *
               * @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;
              }

              /**
               * 將兩個(gè)ASCII字符合成一個(gè)字節(jié);
               * 如:"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,以每兩個(gè)字符分割轉(zhuǎn)換為16進(jìn)制形式
               * 如:"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 2010-03-02 11:04 George-tech 閱讀(478) 評(píng)論(0)  編輯  收藏 所屬分類: java.complex

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 闽侯县| 保定市| 措勤县| 乐至县| 始兴县| 女性| 南溪县| 芮城县| 邛崃市| 呈贡县| 手游| 临沭县| 唐河县| 渭南市| 荥经县| 九龙城区| 五莲县| 麻栗坡县| 安丘市| 朝阳县| 甘孜| 岐山县| 正阳县| 河津市| 页游| 临澧县| 霍林郭勒市| 政和县| 略阳县| 姜堰市| 山西省| 若尔盖县| 安吉县| 岚皋县| 土默特左旗| 马龙县| 武定县| 察哈| 达州市| 武功县| 大足县|