java 進位轉換 協助類(二進位、十進位、十六進位、字串、byte[]互相轉換)

來源:互聯網
上載者:User
package com.mas.fhk.util;import java.io.ByteArrayInputStream;  import java.io.ByteArrayOutputStream;  import java.io.IOException;  import java.io.ObjectInputStream;  import java.io.ObjectOutputStream;  import java.io.Serializable;  import java.io.UnsupportedEncodingException;import java.security.MessageDigest;  import java.security.NoSuchAlgorithmException;  import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;//協助類public class ConvertUtil { public final static char[] BToA = "0123456789abcdef".toCharArray();        private ConvertUtil() {        }          public static String getNowDate(){    try {    java.util.Calendar c=java.util.Calendar.getInstance();       java.text.SimpleDateFormat f=new java.text.SimpleDateFormat("MM月dd日  HH:mm");       return f.format(c.getTime()); } catch (Exception e) {throw e;}    }        /**     * int值轉換為4位的十六進位     * @param s     * @return     */    public static String format4Hex(int n){    try {    String str=Integer.toHexString(n);    int len=str.length();    String hexStr = "";    for(int i=len;i<4;i++){       if(i==len)       hexStr="0";       else       hexStr=hexStr+"0";    }    return hexStr+str.toUpperCase();} catch (Exception e) {throw e;}  }        /**     * 二進位轉換成十六進位字串     * @param binaryStr     * @return     */    public static String BINTOHEX(String binstr){    try {     int len = binstr.length() / 4;  String res = "";  for(int i = 0;i < len;i++){   char[] ch = new char[4];   binstr.getChars(i * 4, i * 4 + 4, ch, 0);   res = res + switchS(trans1(ch));  }  return res;} catch (Exception e) {throw e;}} public static int trans1(char[] ch){try {int sum = 0;for(int i = 0;i < 4;i++){int y = 8;   if(ch[i] == '1'){    for(int j = 1;j <= i;j++){     y = y / 2;    }    sum = sum + y;   }    }return sum;} catch (Exception e) {throw e;} }  public static String switchS(int i){ try { String s = "";  switch(i){   case 10:    s = "A";    break;   case 11:    s = "B";    break;   case 12:    s = "C";    break;   case 13:    s = "D";    break;   case 14:    s = "E";    break;   case 15:    s = "F";   default:    s = "" + i;   }  return s;} catch (Exception e) {throw e;} }  /**  * 十六進位轉換成二進位  * @param str  * @return  */    public static  String HEXTOBIN(String str)    {    try {    String resultStr = "";        String str2 = "";        for (int i = 0; i < str.length(); i++)        {            switch (str.substring(i, i+1).toUpperCase())            {                case "0":                    str2 = "0000";                    break;                case "1":                    str2 = "0001";                    break;                case "2":                    str2 = "0010";                    break;                case "3":                    str2 = "0011";                    break;                case "4":                    str2 = "0100";                    break;                case "5":                    str2 = "0101";                    break;                case "6":                    str2 = "0110";                    break;                case "7":                    str2 = "0111";                    break;                case "8":                    str2 = "1000";                    break;                case "9":                    str2 = "1001";                    break;                case "A":                    str2 = "1010";                    break;                case "B":                    str2 = "1011";                    break;                case "C":                    str2 = "1100";                    break;                case "D":                    str2 = "1101";                    break;                case "E":                    str2 = "1110";                    break;                case "F":                    str2 = "1111";                    break;            }            resultStr += str2;        }        return resultStr;} catch (Exception e) {throw e;}    }  /**  * ASCII碼轉換為十六進位  * @param ch  * @return  */ public static String ASCTOHEX(String ch){ try { char[] chararr = ch.toCharArray();String str = "";for(int i = 0;i<chararr.length;i++){int a=(int)chararr[i];     //ASCII     str +=Integer.toString(a,16);   //十六進位  }  return str.toUpperCase();} catch (Exception e) {throw e;}}  /**  * 十六進位字串轉換為ASCII  * @param hexStr  * @return  */ public static String HEXTOASC(String hex){ try { StringBuilder ascStr = new StringBuilder();  StringBuilder temp = new StringBuilder();  //每2個為一群組轉換為ASCII字元  for( int i=0; i<hex.length()-1; i+=2 ){      String output = hex.substring(i, (i + 2));      int decimal = Integer.parseInt(output, 16);      ascStr.append((char)decimal);      temp.append(decimal);  }  return ascStr.toString();} catch (Exception e) {throw e;} } /**  * 漢字轉換為十六進位  * @param content  * @return  */ public static String GBTOHEX(String content){//將漢字轉換為16進位數 try { String enUnicode=null;  for(int i=0;i<content.length();i++){   if(i==0){         enUnicode=getHexString(Integer.toHexString(content.charAt(i)).toUpperCase())+"20";      }else{      enUnicode=enUnicode+getHexString(Integer.toHexString(content.charAt(i)).toUpperCase())+"20";      }  }  return enUnicode;} catch (Exception e) {throw e;} }  /**  * 16進位數轉換為漢字  * @param strHex  * @return  */ public static String HEXTOGB(String strHex){ try { String AA = "";         strHex = strHex.replace("00", "");         String[] s = strHex.split("20");         for (int i = 0; i < s.length; i++)         {             AA += ZF16ToZF(s[i]);         }         return AA;} catch (Exception e) {throw e;} } public  static String ZF16ToZF(String str16){try { String ZFStr = "";         if(str16 == "")         {             return"";         }         if (str16.length() % 2 != 0)         {             str16 += "20";         }         byte[] bi = new byte[str16.length() / 2];         for (int i = 0; i < bi.length; i ++)         {             bi[i] = hexStringToBytes(str16.substring(i*2, i*2+2));         }         try {ZFStr = new String(bi,"GBK");} catch (UnsupportedEncodingException e) {e.printStackTrace();}         return ZFStr;} catch (Exception e) {throw e;} }public static byte hexStringToBytes(String hexString) {  try {hexString = hexString.toUpperCase();      char[] hexChars = hexString.toCharArray();      byte d;      d = (byte) (charToByte(hexChars[0]) << 4 | charToByte(hexChars[1]));      return d;  } catch (Exception e) {throw e;}}  public  static byte charToByte(char c) {   return (byte) "0123456789ABCDEF".indexOf(c);  }    /**  * 字串轉換為4位字串  14----0014  * @param hexString  * @return  */ public static String getHexString(String hexString){ try { String hexStr="";      for(int i=hexString.length();i<4;i++){       if(i==hexString.length())       hexStr="0";       else       hexStr=hexStr+"0";      }      return hexStr+hexString;} catch (Exception e) {throw e;} }       /**      * 把16進位字串轉換成位元組數組 byte[]     * @param hex      * @return      */      public static byte[] hexStringToByte(String hex) {     try {       int len = (hex.length() / 2);             byte[] result = new byte[len];             char[] achar = hex.toCharArray();             for (int i = 0; i < len; i++) {                 int pos = i * 2;                 result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));             }             return result;  } catch (Exception e) {throw e;}    }              private static byte toByte(char c) {          byte b = (byte) "0123456789ABCDEF".indexOf(c);          return b;      }        /**      * 把位元組數組byte[]轉換成16進位字串      * @param bArray      * @return      */      public static final String bytesToHexString(byte[] bArray) {      try {    if(bArray == null )          {              return "";          }          StringBuffer sb = new StringBuffer(bArray.length);          String sTemp;          for (int i = 0; i < bArray.length; i++) {              sTemp = Integer.toHexString(0xFF & bArray[i]);              if (sTemp.length() < 2)                  sb.append(0);              sb.append(sTemp.toUpperCase());          }          return sb.toString();  } catch (Exception e) {throw e;}    }          /**     * 從一個byte[]數組中截取一部分     * @param src     * @param begin     * @param count     * @return     */    public static byte[] subBytes(byte[] src, int begin, int count) {        byte[] bs = new byte[count];        for (int i=begin; i<begin+count; i++) bs[i-begin] = src[i];        return bs;    }      /**      * 把位元組數群組轉換為對象      *       * @param bytes      * @return      * @throws IOException      * @throws ClassNotFoundException      */      public static final Object bytesToObject(byte[] bytes) throws IOException,              ClassNotFoundException {          ByteArrayInputStream in = new ByteArrayInputStream(bytes);          ObjectInputStream oi = new ObjectInputStream(in);          Object o = oi.readObject();          oi.close();          return o;      }        /**      * 把可序列化對象轉換成位元組數組      *       * @param s      * @return      * @throws IOException      */      public static final byte[] objectToBytes(Serializable s) throws IOException {          ByteArrayOutputStream out = new ByteArrayOutputStream();          ObjectOutputStream ot = new ObjectOutputStream(out);          ot.writeObject(s);          ot.flush();          ot.close();          return out.toByteArray();      }        public static final String objectToHexString(Serializable s)              throws IOException {          return bytesToHexString(objectToBytes(s));      }        public static final Object hexStringToObject(String hex)              throws IOException, ClassNotFoundException {          return bytesToObject(hexStringToByte(hex));      }        /**      * @函數功能: BCD碼轉為10進位串(阿拉伯資料)      * @輸入參數: BCD碼      * @輸出結果: 10進位串      */      public static String bcd2Str(byte[] bytes) {          StringBuffer temp = new StringBuffer(bytes.length * 2);            for (int i = 0; i < bytes.length; i++) {              temp.append((byte) ((bytes[i] & 0xf0) >>> 4));              temp.append((byte) (bytes[i] & 0x0f));          }          return temp.toString().substring(0, 1).equalsIgnoreCase("0") ? temp                  .toString().substring(1) : temp.toString();      }        /**      * @函數功能: 10進位串轉為BCD碼      * @輸入參數: 10進位串      * @輸出結果: BCD碼      */      public static byte[] str2Bcd(String asc) {          int len = asc.length();          int mod = len % 2;            if (mod != 0) {              asc = "0" + asc;              len = asc.length();          }            byte abt[] = new byte[len];          if (len >= 2) {              len = len / 2;          }            byte bbt[] = new byte[len];          abt = asc.getBytes();          int j, k;            for (int p = 0; p < asc.length() / 2; p++) {              if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {                  j = abt[2 * p] - '0';              } else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {                  j = abt[2 * p] - 'a' + 0x0a;              } else {                  j = abt[2 * p] - 'A' + 0x0a;              }                if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {                  k = abt[2 * p + 1] - '0';              } else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {                  k = abt[2 * p + 1] - 'a' + 0x0a;              } else {                  k = abt[2 * p + 1] - 'A' + 0x0a;              }                int a = (j << 4) + k;              byte b = (byte) a;              bbt[p] = b;          }          return bbt;      }        public static String BCD2ASC(byte[] bytes) {     try {     StringBuffer temp = new StringBuffer(bytes.length * 2);                 for (int i = 0; i < bytes.length; i++) {               int h = ((bytes[i] & 0xf0) >>> 4);               int l = (bytes[i] & 0x0f);               temp.append(BToA[h]).append(BToA[l]);           }           return temp.toString(); } catch (Exception e) {throw e;}    }        /**      * 兩字元數組異或      */      public static byte[] byteArrXor(byte[] arr1, byte[] arr2, int len){      try {     byte[] dest = new byte[len];           if((arr1.length < len) || (arr2.length < len)){               return null;           }           for(int i = 0;i < len;i++){               dest[i] = (byte)(arr1[i] ^ arr2[i]);           }           return dest;  } catch (Exception e) {throw e;}    }          /**     * 擷取明天的日期     * @return     */    @SuppressWarnings("static-access")public static String getTomorrowDate() {Date date=new Date();//取時間Calendar calendar = new GregorianCalendar();calendar.setTime(date);calendar.add(calendar.DATE,1);//把日期往後增加一天.整數往後推,負數往前移動date=calendar.getTime(); //這個時間就是日期往後推一天的結果SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");String dateString = formatter.format(date);return dateString;}        public static void main(String[] args) {//String s = "4130";//System.out.println(HEXTOASC(s));    System.out.println(getNowDate());}      }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.