package com.dayu.baselibrary.tools; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Created by zuo on 2018/12/2. */ public class Utils { /** * 字符串转换成为16进制(无需Unicode编码) * * @param str * @return */ public static String str2HexStr(String str) { char[] chars = "0123456789ABCDEF".toCharArray(); StringBuilder sb = new StringBuilder(""); byte[] bs = str.getBytes(); int bit; for (int i = 0; i < bs.length; i++) { bit = (bs[i] & 0x0f0) >> 4; sb.append(chars[bit]); bit = bs[i] & 0x0f; sb.append(chars[bit]); // sb.append(' '); } return sb.toString().trim(); } /** * 字节数组转16进制删除为0的 * * @param bytes 需要转换的byte数组 * @return 转换后的Hex字符串 */ public static String bytesToHexClean0(byte[] bytes) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < bytes.length; i++) { String hex = Integer.toHexString(bytes[i] & 0xFF); if ("0".equals(hex)) break; if (hex.length() < 2) { sb.append(0); } sb.append(hex); } return sb.toString(); } /** * 16进制转Byte * * @param hex * @return */ public static byte[] hexStringToByte(String hex) { 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; } private static int toByte(char c) { byte b = (byte) "0123456789ABCDEF".indexOf(c); return b; } /** * 16进制转换成为string类型字符串 * * @param s * @return */ public static String hexStringToString(String s) { if (s == null || s.equals("")) { return null; } s = s.replace(" ", ""); byte[] baKeyword = new byte[s.length() / 2]; for (int i = 0; i < baKeyword.length; i++) { try { baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16)); } catch (Exception e) { e.printStackTrace(); } } try { s = new String(baKeyword, "UTF-8"); new String(); } catch (Exception e1) { e1.printStackTrace(); } return s; } /** * byte转bit * * @param by * @return */ public static String getBit(byte by) { StringBuffer sb = new StringBuffer(); sb.append((by >> 7) & 0x1) .append((by >> 6) & 0x1) .append((by >> 5) & 0x1) .append((by >> 4) & 0x1) .append((by >> 3) & 0x1) .append((by >> 2) & 0x1) .append((by >> 1) & 0x1) .append((by >> 0) & 0x1); return sb.toString(); } /** * byte转bit * * @param by * @return */ public static String getBit(byte[] by) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < by.length; i++) { sb.append((by[i] >> 7) & 0x1) .append((by[i] >> 6) & 0x1) .append((by[i] >> 5) & 0x1) .append((by[i] >> 4) & 0x1) .append((by[i] >> 3) & 0x1) .append((by[i] >> 2) & 0x1) .append((by[i] >> 1) & 0x1) .append((by[i] >> 0) & 0x1); sb.append("\n"); } return sb.toString(); } /** * bit转byte * * @param bit * @return */ public static byte bitToByte(String bit) { int re, len; if (null == bit) { return 0; } len = bit.length(); if (len != 4 && len != 8) { return 0; } if (len == 8) {// 8 bit处理 if (bit.charAt(0) == '0') {// 正数 re = Integer.parseInt(bit, 2); } else {// 负数 re = Integer.parseInt(bit, 2) - 256; } } else {//4 bit处理 re = Integer.parseInt(bit, 2); } return (byte) re; } /** * short转byte数组 * * @param x * @return */ public static byte[] short2Byte(short x) { byte high = (byte) (0x00FF & (x >> 8));//定义第一个byte byte low = (byte) (0x00FF & x);//定义第二个byte byte[] bytes = new byte[2]; bytes[0] = high; bytes[1] = low; return bytes; } /** * short转byte * * @param x * @return */ public static byte[] short2ByteNew(short x) { byte high = (byte) (0x00FF & (x >> 8));//定义第一个byte byte low = (byte) (0x00FF & x);//定义第二个byte byte[] bytes = new byte[2]; bytes[1] = high; bytes[0] = low; return bytes; } /** * byte[]转short * * @param bytes * @return */ public static short byte2shortNew(byte[] bytes) { byte high = bytes[1]; byte low = bytes[0]; short z = (short) (((high & 0x00FF) << 8) | (0x00FF & low)); return z; } //byte 数组与 int 的相互转换 public static int byteArrayToInt(byte[] b) { return b[0] & 0xFF | (b[1] & 0xFF) << 8 | (b[2] & 0xFF) << 16 | (b[3] & 0xFF) << 24; } public static byte[] intToByteArray(int a) { return new byte[]{ (byte) (a & 0xFF), (byte) ((a >> 8) & 0xFF), (byte) ((a >> 16) & 0xFF), (byte) ((a >> 24) & 0xFF) }; } public static String intToString(int a) { StringBuffer data = new StringBuffer(); byte[] byteData = new byte[]{ (byte) ((a >> 24) & 0xFF), (byte) ((a >> 16) & 0xFF), (byte) ((a >> 8) & 0xFF), (byte) (a & 0xFF) }; return data.append(getBit(byteData[0])).append(getBit(byteData[1])).append(getBit(byteData[2])).append(getBit(byteData[3])).toString(); } /** * 获取4位的用户号 * * @return */ public static String getName(String name) { String data = name; for (int i = 0; i < 4 - name.length(); i++) { data = "0" + data; } return data; } /** * @param data * @param total 一共多少位 * @return */ public static int bitToInt(byte[] data, int total) { String strData = intToString(byteArrayToInt(data)); strData = strData.substring(0, total); for (int i = 0; i < 32 - total; i++) { strData = "0" + strData; } return bitToInt(strData); } public static int bitToInt(String data) { if (data.length() == 32) { byte[] byteData = new byte[4]; byteData[0] = bitToByte(data.substring(24)); byteData[1] = bitToByte(data.substring(16, 24)); byteData[2] = bitToByte(data.substring(8, 16)); byteData[3] = bitToByte(data.substring(0, 8)); return byteArrayToInt(byteData); } else { return 0; } } //判断是否为有效身份证号 public static boolean check(String str) { String patt=""; if(str.length()==15) patt="^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$"; else if(str.length()==18) patt="^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$"; else return false; Pattern r = Pattern.compile(patt); Matcher matcher = r.matcher(str); return matcher.find(); } public static void main(String[] args) { Utils.getBit((byte) 49); } }