一些经常用到的数据转换函数

/****************** HexString to byte*****/ public static byte[] hexStringToBytes(String hexString) { if (hexString == null || hexString.equals("")) { return null; } String newStr=null; if((hexString.length())%2!=0) newStr="0"+hexString; else newStr=hexString; newStr = newStr.toUpperCase(); int length = newStr.length() / 2; char[] hexChars = newStr.toCharArray(); byte[] d = new byte[length]; for (int i = 0; i < length; i++) { int pos = i * 2; d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); } return d; } /**************************** Convert char to byte***********************/ public static byte charToByte(char c) { return (byte) "0123456789ABCDEF".indexOf(c); } /**************************** byte[] to int***********************/ public static int bytesToInt(byte[] bs) { String dataLenStr=CommonMethods.bytesToHexString(bs); int commLen=Integer.parseInt(dataLenStr,16); public static String getNowTime() { Date dt=new Date(); SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); String nowTime=""; nowTime= df.format(dt); return nowTime; } /****************** byte stream to HexString**********************/ public static String bytesToHexString(byte[] bArray) { StringBuffer sb = new StringBuffer(bArray.length); String sTemp; for (int i = 0; i < bArray.length; i++) { sTemp = Integer.toHexString((int)(0xFF & bArray[i])); if (sTemp.length() < 2) sb.append(0); sb.append(sTemp.toUpperCase()); } return sb.toString(); } return commLen; }

你可能感兴趣的:(c,Date,String,Stream,null,byte)