java byte 与操作_Java Byte 操作

publicclassByteUtil {/*** Convert a String of hexadecimal digits into the corresponding byte array

* by encoding each two hexadecimal digits as a byte.

*

*@paramdigits

*            Hexadecimal digits representation

*

*@exceptionIllegalArgumentException

*                if an invalid hexadecimal digit is found, or the input

*                string contains an odd number of hexadecimal digits*/publicstaticbyte[] hexStringToByteArray(String digits) {

ByteArrayOutputStream baos=newByteArrayOutputStream();for(inti=0; i=digits.length()) {thrownewIllegalArgumentException("hexUtil.odd");

}charc2=digits.charAt(i+1);byteb=0;if((c1>='0')&&(c1<='9'))

b+=((c1-'0')*16);elseif((c1>='a')&&(c1<='f'))

b+=((c1-'a'+10)*16);elseif((c1>='A')&&(c1<='F'))

b+=((c1-'A'+10)*16);elsethrownewIllegalArgumentException("hexUtil.bad");if((c2>='0')&&(c2<='9'))

b+=(c2-'0');elseif((c2>='a')&&(c2<='f'))

b+=(c2-'a'+10);elseif((c2>='A')&&(c2<='F'))

b+=(c2-'A'+10);elsethrownewIllegalArgumentException("hexUtil.bad");

baos.write(b);

}return(baos.toByteArray());

}/*** Convert a byte array into a printable format containing a String of

* hexadecimal digit characters (two per byte).

*@seethis method consume too much memory, especailly when large byte array.

*

*@parambytes

*            Byte array representation*/publicstaticString convertByteArrayToHexStr(bytebytes[]) {

StringBuffer sb=newStringBuffer(bytes.length*2);for(inti=0; i

sb.append(convertDigitToHexChar((int) (bytes[i]>>4)));

sb.append(convertDigitToHexChar((int) (bytes[i]&0x0f)));

}return(sb.toString());

}/*** convert byte array into hex string, with , as seperator

*@seethis method consume too much memory, especailly when large byte array.

*@parambytes

*@return*/publicstaticString convertByteArrayToHexStr2(bytebytes[]) {

StringBuffer sb=newStringBuffer();for(inti=0; i

sb.append(convertDigitToHexChar((int) (bytes[i]>>4)));

sb.append(convertDigitToHexChar((int) (bytes[i]&0x0f)));

sb.append(",");

}return(sb.toString().substring(0,sb.length()-1));

}/*** Convert the specified value (0 .. 15, i.e. 4bits) to the corresponding hexadecimal

* digit.

*

*@paramvalue

*            Value to be converted*/privatestaticcharconvertDigitToHexChar(intvalue) {

value=value&0x0f;if(value>=10)return((char) (value-10+'a'));elsereturn((char) (value+'0'));

}publicstaticString convertByteToHexStr(bytevalue) {

StringBuffer sb=newStringBuffer();

sb.append(convertDigitToHexChar((int) (value>>4)));

sb.append(convertDigitToHexChar((int) (value&0x0f)));return(sb.toString());

}/*** Convert an int to a byte array

*

*@paramvalue  int

*@returnbyte[]*/publicstaticbyte[] intToByteArray(intvalue) {byte[] b=newbyte[4];for(inti=0; i<4; i++) {intoffset=(b.length-1-i)*8;

b[i]=(byte) ((value>>offset)&0xFF);//}returnb;

}/*** change byte array into a unsigned byte array

*@paramsource

*@return*/publicstaticbyte[] toUnsignedByteArray(byte[] source){//ByteUtil.printByteArray(source);//to keep value in the 0-255intmodel=256;if(source==null||source.length==0){returnnewbyte[0];

}byte[] dest=newbyte[source.length];for(inti=0;i

dest[i]=(byte)tmp;

}returndest;

}/*** Convert the byte array to an int starting from the given offset.

*

*@paramb

*            The byte array

*@paramoffset

*

*@returnThe integer*/publicstaticintbyteArrayToInt(byte[] b) {if(b.length>4){thrownewRuntimeException("more than 4 byte");

}intvalue=0;for(inti=0; i

value+=(b[i]&0xFF)<

}returnvalue;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值