字符之間的轉換

/**
  * 提取字符串中爲字母的字符
  */
 public static String returnResultMultiple(String str) {
  String string = "";
  if (str.equals("")) {
   return "";
  }
  for (int i = 0; i < str.length(); i++) {
   char ch = str.charAt(i);
   if (Character.isLetter(ch)) {
    string = string + ch;
   }
  }
  return string;
 }
 /**
  * 提取字符串中爲數字的字符
  */
 public static String returnNumber(String str) {
  String str2 = "";
  if (str != null && !"".equals(str)) {
   for (int i = 0; i < str.length(); i++) {
    if (str.charAt(i) >= 48 && str.charAt(i) <= 57) {
     str2 += str.charAt(i);
    }
   }
   return str2;
  }
  return str2;
 }

/**
  * 將byte[]轉爲String
  *
  * @param b
  * @return
  */
 @SuppressLint("DefaultLocale")
 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;
 }
 /**
  * 將String轉爲byte[]
  *
  * @param message
  * @return
  */
 private byte[] getHexBytes(String message) {
  int len = message.length() / 2;
  char[] chars = message.toCharArray();
  String[] hexStr = new String[len];
  byte[] bytes = new byte[len];
  for (int i = 0, j = 0; j < len; i += 2, j++) {
   hexStr[j] = "" + chars[i] + chars[i + 1];
   bytes[j] = (byte) Integer.parseInt(hexStr[j], 16);
  }
  return bytes;
 }
//將int轉爲16進制字符串
String string = Integer.toHexString(int );
//將長度補齊,(數據,長度,用什麼來補)
String padln = StringUtils.leftPad(string, 8, "0");

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章