字符之间的转换

来源:互联网 发布:淘宝哪家精工机械杆好 编辑:程序博客网 时间:2024/06/10 03:09
/**
  * 提取字符串中为字母的字符
  */
 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");

1 0
原创粉丝点击