2019년 3월 25일 월요일

Java 형변환 예제

Google+ 서비스 종료로 인해 과거의 기록들을 이곳으로 옮겨둔다.

Java 형변환에 대한 예.

String to Int

1. Integer.parseInt(string);
2. Integer.valueOf(string);

Integer.parseInt()
      int Num = Integer.parseInt(Str);

--------------------------------------------------------------------------------------------------
Int to String

1. String.valueOf(number)
2. Integer.toString(number)
3. number + ""

-> String.valueOf(number) 와 Interger.toString(number) 는 동일.
-> number+""  는 다른것에 비해 속도가 느리다.

--------------------------------------------------------------------------------------------------
Object to Byte Array

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos); 
out.writeObject(yourObject);
byte[] yourBytes = bos.toByteArray();

--------------------------------------------------------------------------------------------------
Byte Array to Object

ByteArrayIntputSream bis = new ByteArrayInputStream(yourBytes);
ObjectInput in = new ObjectInputStream(bis);
Object o = in.readObject();

--------------------------------------------------------------------------------------------------
Int to Byte Array

public static byte[] i2b_le(int myInteger){
    return ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(myInteger).array();
}

public static byte[] i2b_be(int myInteger){
    return ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN).putInt(myInteger).array();
}

--------------------------------------------------------------------------------------------------
Byte Array to Int

public static int b2i_le(byte [] byteBarray){
   return ByteBuffer.wrap(byteBarray).order(ByteOrder.LITTLE_ENDIAN).getInt();
}

public static int b2i_be(byte [] byteBarray){
    return ByteBuffer.wrap(byteBarray).order(ByteOrder.BIG_ENDIAN).getInt();
}

--------------------------------------------------------------------------------------------------
String to Byte Array

byte[] b = string.getBytes();
byte[] b = string.getBytes(Charset.forName("UTF-8"));

--------------------------------------------------------------------------------------------------
Byte Array to String

byte [] t_sub3= getbytes(rcvData,8,rcvData.length-8);

String str  = new String(t_sub3,0,t_sub3.length);

* t_sub3은 배열 이름, 0~t_sub3.length는 offset 이다.

--------------------------------------------------------------------------------------------------
Byte Array 부분 복사하기

public static void arraycopy(Object src,
             int srcPos,
             Object dest,
             int destPos,
             int length)

Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.
A subsequence of array components are copied from the source array referenced by src to the destination array referenced by dest.
The number of components copied is equal to the length argument.
The components at positions srcPos through srcPos+length-1 in the source array are copied into positions destPos through destPos+length-1, respectively, of the destination array.

--------------------------------------------------------------------------------------------------
ArrayList 사용 method

myList.size() : 객체의 size 확인
myList.indexOf(d) : 객체의 index 확인
myList.contains(d) : 객체 존재여부 확인
myList.isEmpty() :  ArrayList가 비어있는지 확인
myList.remove(d) : ArrayList에 있는 객체 제거

댓글 없음:

댓글 쓰기