'2008/09/30'에 해당되는 글 2건

  1. ByteBuffer to String (2) 2008/09/30
  2. Convert int -> byte array and byte array -> int 2008/09/30

ByteBuffer to String

from Java 2008/09/30 13:18
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.put("aabcde".getBytes());
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
String s = new String(bytes);

위가 아니라 아래처럼 해야 됩니다. ^^;;

ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.put("aabcde".getBytes());
byte[] bytes = new byte[buffer.position()];
buffer.flip();       
buffer.get(bytes);
String s = new String(bytes);
System.out.println(s);
public int byteArrayToInt(byte [] b) {
return (b[0] << 24) + ((b[1] & 0xFF) << 16) + ((b[2] & 0xFF) << 8) + (b[3] & 0xFF);
}


public byte[] intToByteArray(int value) {
  return new byte[] {(byte)(value >>> 24), (byte)(value >>> 16), (byte)(value >>> 8), (byte)value};
}


'Java' 카테고리의 다른 글

Get and put unsigned values to a ByteBuffer  (0) 2008/10/07
ByteBuffer to String  (2) 2008/09/30
Convert int -> byte array and byte array -> int  (0) 2008/09/30
POJO와 관련된 용어들..  (1) 2008/09/24
File 내용 추가하기..  (0) 2008/09/24
Tag // byte array, int, java