public class StrToByte {
/**
* 将yyMMddhhmmss格式日期字符串转换成byte[] <br/>
* 其中yy取最大为62即2000--2062
*
* @author linfenliang
* @date 2012-6-19
* @version V1.0.0
* @param dateStr
* :日期字符串
* @return byte[]
*/
public static byte[] strToByte(String dateStr) {
byte[] timeByte = new byte[4];
int[] tempTime = new int[6];
tempTime[0] = Integer.parseInt(dateStr.substring(0, 2));// year
tempTime[1] = Integer.parseInt(dateStr.substring(2, 4));// month
tempTime[2] = Integer.parseInt(dateStr.substring(4, 6));// day
tempTime[3] = Integer.parseInt(dateStr.substring(6, 8));// hh
tempTime[4] = Integer.parseInt(dateStr.substring(8, 10));// mm
tempTime[5] = Integer.parseInt(dateStr.substring(10, 12));// ss
timeByte[0] = (byte) ((tempTime[0] << 2) + (tempTime[1] >> 2));
timeByte[1] = (byte) (((tempTime[1] & 3) << 6) + (tempTime[2] << 1) + (tempTime[3] >> 4 & 1));
timeByte[2] = (byte) (((tempTime[3] & 15) << 4) + ((tempTime[4] >> 2) & 15));
timeByte[3] = (byte) (((tempTime[4] & 3) << 6) + tempTime[5]);
return timeByte;
}
/**
* 将byte数组转换成16进制字符串
*
* @author linfenliang
* @date 2012-6-19
* @version V1.0.0
* @param b:byte[]
* @return String
*/
public static String byteToHexStr(byte[] b) {
StringBuffer sBuffer = new StringBuffer();
for (byte bb : b) {
sBuffer.append(Integer.toHexString(bb & 0xff));
}
return sBuffer.toString();
}
/**
*
* @author linfenliang
* @date 2012-6-19
* @version V1.0.0
* @param args
* void
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
byte[] b = strToByte("120509104354");
System.out.println(byteToHexStr(b));
}
}
java的移位运算实例
原创
©著作权归作者所有:来自51CTO博客作者1058106015的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:java数据库连接池技术浅析
下一篇:java的移位运算
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
移位运算
移位运算1.逻辑移位2.算数移位3.循环移位
计算机组成原理 循环移位 移位运算 逻辑移位 其他