Android Open Source - BluetoothSppPro C Hex Conver






From Project

Back to project page BluetoothSppPro.

License

The source code is released under:

Apache License

If you think the Android project BluetoothSppPro listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package mobi.dzs.android.util;
//from   w  ww  . j a v  a  2 s  .  c om
import java.util.Locale;

/**
 * 16????String/Byte???????
 * @author JerryLi
 * @email lijian@dzs.mobi
 * @data 2011-10-16
 * */
public class CHexConver
{
  private final static char[] mChars = "0123456789ABCDEF".toCharArray();
  private final static String mHexStr = "0123456789ABCDEF";  
  /** 
   * ??16???????????
   * @param sHex String 16?????
   * @return boolean
   */  
  public static boolean checkHexStr(String sHex){  
      String sTmp = sHex.toString().trim().replace(" ", "").toUpperCase(Locale.US);
      int iLen = sTmp.length();
      
      if (iLen > 1 && iLen%2 == 0){
        for(int i=0; i<iLen; i++)
          if (!mHexStr.contains(sTmp.substring(i, i+1)))
            return false;
        return true;
      }
      else
        return false;
    }
  
  /** 
   * ??????????????????
   * @param str String ??????ASCII???
   * @return String ???Byte????????: [61 6C 6B]
   */  
    public static String str2HexStr(String str){  
        StringBuilder sb = new StringBuilder();
        byte[] bs = str.getBytes();  
        
        for (int i = 0; i < bs.length; i++){  
            sb.append(mChars[(bs[i] & 0xFF) >> 4]);  
            sb.append(mChars[bs[i] & 0x0F]);
            sb.append(' ');
        }  
        return sb.toString().trim();  
    }
    
    /** 
     * ??????????????? ASCII???
   * @param str String Byte???
   * @return String ??????
     */  
    public static String hexStr2Str(String hexStr){  
      hexStr = hexStr.toString().trim().replace(" ", "").toUpperCase(Locale.US);
        char[] hexs = hexStr.toCharArray();  
        byte[] bytes = new byte[hexStr.length() / 2];  
        int iTmp = 0x00;;  

        for (int i = 0; i < bytes.length; i++){  
          iTmp = mHexStr.indexOf(hexs[2 * i]) << 4;  
          iTmp |= mHexStr.indexOf(hexs[2 * i + 1]);  
            bytes[i] = (byte) (iTmp & 0xFF);  
        }  
        return new String(bytes);  
    }
    
    /**
     * bytes???????????????
     * @param b byte[] byte??
     * @param iLen int ?????N?????? N=iLen
     * @return String ???Byte???????
     */
  public static String byte2HexStr(byte[] b, int iLen){
        StringBuilder sb = new StringBuilder();
        for (int n=0; n<iLen; n++){
          sb.append(mChars[(b[n] & 0xFF) >> 4]);
          sb.append(mChars[b[n] & 0x0F]);
            sb.append(' ');
        }
        return sb.toString().trim().toUpperCase(Locale.US);
    }
    
    /**
     * bytes????????Byte?
     * @param src String Byte???????Byte???????(????:0-9 A-F)
     * @return byte[]
     */
  public static byte[] hexStr2Bytes(String src){
      /*?????????????*/
      src = src.trim().replace(" ", "").toUpperCase(Locale.US);
      //?????????
      int m=0,n=0;
        int iLen=src.length()/2; //????
        byte[] ret = new byte[iLen]; //???????
        
        for (int i = 0; i < iLen; i++){
            m=i*2+1;
            n=m+1;
            ret[i] = (byte)(Integer.decode("0x"+ src.substring(i*2, m) + src.substring(m,n)) & 0xFF);
        }
        return ret;
    }

    /**
     * String??????????unicode?String
     * @param strText String ?????
     * @return String ???unicode??????
     * @throws Exception
     */
    public static String strToUnicode(String strText)
      throws Exception
    {
        char c;
        StringBuilder str = new StringBuilder();
        int intAsc;
        String strHex;
        for (int i = 0; i < strText.length(); i++){
            c = strText.charAt(i);
            intAsc = (int) c;
            strHex = Integer.toHexString(intAsc);
            if (intAsc > 128)
              str.append("\\u");
            else // ??????????00
              str.append("\\u00");
            str.append(strHex);
        }
        return str.toString();
    }
    
    /**
     * unicode?String??????String????
     * @param hex String 16?????? ???unicode?2byte?
     * @return String ?????
     * @see CHexConver.unicodeToString("\\u0068\\u0065\\u006c\\u006c\\u006f")
     */
    public static String unicodeToString(String hex){
        int t = hex.length() / 6;
        int iTmp = 0;
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < t; i++){
            String s = hex.substring(i * 6, (i + 1) * 6);
            // ?16???string??int
            iTmp = (Integer.valueOf(s.substring(2, 4), 16) << 8) | Integer.valueOf(s.substring(4), 16);
            // ?int???????
            str.append(new String(Character.toChars(iTmp)));
        }
        return str.toString();
    }
}




Java Source Code List

mobi.dzs.android.BLE_SPP_PRO.BaseActivity.java
mobi.dzs.android.BLE_SPP_PRO.BaseCommActivity.java
mobi.dzs.android.BLE_SPP_PRO.actAbout.java
mobi.dzs.android.BLE_SPP_PRO.actByteStream.java
mobi.dzs.android.BLE_SPP_PRO.actCmdLine.java
mobi.dzs.android.BLE_SPP_PRO.actDiscovery.java
mobi.dzs.android.BLE_SPP_PRO.actKeyBoard.java
mobi.dzs.android.BLE_SPP_PRO.actMain.java
mobi.dzs.android.BLE_SPP_PRO.globalPool.java
mobi.dzs.android.bluetooth.BTSerialComm.java
mobi.dzs.android.bluetooth.BluetoothCtrl.java
mobi.dzs.android.bluetooth.BluetoothSppClient.java
mobi.dzs.android.bluetooth.CResourcePV.java
mobi.dzs.android.control.button.ButtonPassListener.java
mobi.dzs.android.control.button.RepeatingButton.java
mobi.dzs.android.storage.CJsonStorage.java
mobi.dzs.android.storage.CKVStorage.java
mobi.dzs.android.storage.CSharedPreferences.java
mobi.dzs.android.util.CHexConver.java
mobi.dzs.android.util.LocalIOTools.java