欢迎来到天天文库
浏览记录
ID:40565971
大小:40.00 KB
页数:5页
时间:2019-08-04
《JAVA二进制字节数组字符十六进制BCD编码转换》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、JAVA二进制字节数组字符十六进制BCD编码转换;importjava.io.*;importjava.security.MessageDigest;importjava.security.NoSuchAlgorithmException;publicclassDataConverter{/*把16进制字符串转换成字节数组*@paramhex*@return*/publicstaticbyte[]hexStringToByte(Stringhex){intlen=(hex.length()/2);byte[]result=newbyte[len];char[]achar=
2、hex.toCharArray();for(inti=0;i3、toByte(achar[pos+1]));}returnresult;}privatestaticbytetoByte(charc){byteb=(byte)"0123456789ABCDEF".indexOf(c);returnb;}/***//***把字节数组转换成16进制字符串*@parambArray*@return*/publicstaticfinalStringbytesToHex4、String(byte[]bArray){StringBuffersb=newStringBuffer(bArray.length);StringsTemp;for(inti=0;i5、xception*@throwsClassNotFoundException*/publicstaticfinalObjectbytesToObject(byte[]bytes)throwsIOException,ClassNotFoundException{ByteArrayInputStreamin=newByteArrayInputStream(bytes);ObjectInputStreamoi=newObjectInputStream(in);Objecto=oi.readObject();oi.close();returno;}/***//***把可序列化对象6、转换成字节数组*@params*@return*@throwsIOException*/publicstaticfinalbyte[]objectToBytes(Serializables)throwsIOException{ByteArrayOutputStreamout=newByteArrayOutputStream();ObjectOutputStreamot=newObjectOutputStream(out);ot.writeObject(s);ot.flush();ot.close();returnout.toByteArray();}publicstati7、cfinalStringobjectToHexString(Serializables)throwsIOException{returnbytesToHexString(objectToBytes(s));}publicstaticfinalObjecthexStringToObject(Stringhex)throwsIOException,ClassNotFoundException{returnbytesToObject(hexStringToByte(hex));}/***//***@函数功能:BCD码转为10进制串(阿拉伯数据)*@输入参数:BCD码*@输出结果8、:10进制串*/publicstaticStringbcd2Str(byte[]bytes){StringBuffertemp=newStringBuffer(bytes.length*2);for(inti=0;i>>4));temp.append((byte)(bytes[i]&0x0f));}returntemp.toString().substring(0,1).equalsIgnoreCase("0")?tem
3、toByte(achar[pos+1]));}returnresult;}privatestaticbytetoByte(charc){byteb=(byte)"0123456789ABCDEF".indexOf(c);returnb;}/***//***把字节数组转换成16进制字符串*@parambArray*@return*/publicstaticfinalStringbytesToHex
4、String(byte[]bArray){StringBuffersb=newStringBuffer(bArray.length);StringsTemp;for(inti=0;i5、xception*@throwsClassNotFoundException*/publicstaticfinalObjectbytesToObject(byte[]bytes)throwsIOException,ClassNotFoundException{ByteArrayInputStreamin=newByteArrayInputStream(bytes);ObjectInputStreamoi=newObjectInputStream(in);Objecto=oi.readObject();oi.close();returno;}/***//***把可序列化对象6、转换成字节数组*@params*@return*@throwsIOException*/publicstaticfinalbyte[]objectToBytes(Serializables)throwsIOException{ByteArrayOutputStreamout=newByteArrayOutputStream();ObjectOutputStreamot=newObjectOutputStream(out);ot.writeObject(s);ot.flush();ot.close();returnout.toByteArray();}publicstati7、cfinalStringobjectToHexString(Serializables)throwsIOException{returnbytesToHexString(objectToBytes(s));}publicstaticfinalObjecthexStringToObject(Stringhex)throwsIOException,ClassNotFoundException{returnbytesToObject(hexStringToByte(hex));}/***//***@函数功能:BCD码转为10进制串(阿拉伯数据)*@输入参数:BCD码*@输出结果8、:10进制串*/publicstaticStringbcd2Str(byte[]bytes){StringBuffertemp=newStringBuffer(bytes.length*2);for(inti=0;i>>4));temp.append((byte)(bytes[i]&0x0f));}returntemp.toString().substring(0,1).equalsIgnoreCase("0")?tem
5、xception*@throwsClassNotFoundException*/publicstaticfinalObjectbytesToObject(byte[]bytes)throwsIOException,ClassNotFoundException{ByteArrayInputStreamin=newByteArrayInputStream(bytes);ObjectInputStreamoi=newObjectInputStream(in);Objecto=oi.readObject();oi.close();returno;}/***//***把可序列化对象
6、转换成字节数组*@params*@return*@throwsIOException*/publicstaticfinalbyte[]objectToBytes(Serializables)throwsIOException{ByteArrayOutputStreamout=newByteArrayOutputStream();ObjectOutputStreamot=newObjectOutputStream(out);ot.writeObject(s);ot.flush();ot.close();returnout.toByteArray();}publicstati
7、cfinalStringobjectToHexString(Serializables)throwsIOException{returnbytesToHexString(objectToBytes(s));}publicstaticfinalObjecthexStringToObject(Stringhex)throwsIOException,ClassNotFoundException{returnbytesToObject(hexStringToByte(hex));}/***//***@函数功能:BCD码转为10进制串(阿拉伯数据)*@输入参数:BCD码*@输出结果
8、:10进制串*/publicstaticStringbcd2Str(byte[]bytes){StringBuffertemp=newStringBuffer(bytes.length*2);for(inti=0;i>>4));temp.append((byte)(bytes[i]&0x0f));}returntemp.toString().substring(0,1).equalsIgnoreCase("0")?tem
此文档下载收益归作者所有