在做crc校验的过程中遇到了iobuffer转换成string的问题,自己找了点资料写了个简单的方法。
- package minaUDP;
- import java.nio.ByteOrder;
- import java.nio.charset.CharacterCodingException;
- import org.apache.mina.core.buffer.IoBuffer;
- publicclass IoBufferTOString {
- publicstatic String ioBufferToString(IoBuffer iobuffer){
- System.out.println("message = " + iobuffer + iobuffer.limit());
- iobuffer.flip();
- byte[] b =newbyte[iobuffer.limit()];
- iobuffer.get(b);
- StringBuffer stringBuffer =new StringBuffer();
- for(int i =0; i < b.length; i++){
- System.out.println("====" + b[i]);
- stringBuffer.append((Byte) b[i]);
- System.out.println(b[i] +"---------" +i);
- }
- return stringBuffer.toString();
- }
- publicstaticvoid main(String[] args)throws CharacterCodingException{
- IoBuffer iobuffer = IoBuffer.allocate(8);
- iobuffer.order(ByteOrder.LITTLE_ENDIAN);
- iobuffer.putChar('z');
- iobuffer.putInt(123);
- String str = ioBufferToString(iobuffer);
- System.out.println(str);
- }
- }