java的移位运算实例

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));
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值