java字符串类型字符将两个字节合并成一个字节

byte[] merge2BytesTo1Byte(String str){
byte[] bytes =new byte[str.length()/2];
char s,e;
for(int i=0;i<str.length();i+=2){
s=str.charAt(i);//第I个索引字符
e=str.charAt(i+1);//第I+1个索引字符
if( s<='9')
{
if(e <='9')
{
bytes[i/2]=(byte)(((s-0x30)<<4)+(e-0x30));
}
else if((e<='z' && e>='a'))
{
bytes[i/2]=(byte)(((s-0x30)<<4)+(e-0x57));//左移4位

}
}
else if(s>'9')
{
if(e<='9')
{
bytes[i/2]=(byte)(((s-0x57)<<4)+(e-0x30));
}
else if((s<='z' && e>='a'))
{
bytes[i/2]=(byte)(((s-0x57)<<4)+(e-0x57));
}
}
}
return bytes;
}

猜你喜欢

转载自zflyhigh.iteye.com/blog/2323196