<< 1 byte를 byte 배열 형으로 선언>>
byte[] packet = ByteBuffer.allocate(1).put(command).array();
<< byte (1byte) -> int >>
1 2 3 | public int byte2Int(byte src) { return src & 0xFF; } | cs |
※ 자바에서 byte는 sign이기 때문에 범위 표현이 -128 ~ 127 이다.
그래서 (byte) 0xFF를 int에 아무 것도 안하고 그대로 대입하면 -1이 나와 버린다.
<< byte (4byte) -> int >>
1 2 3 4 5 6 7 8 | public int byte2Int(byte[] source) { int source_1 = source[0] & 0xFF; int source_2 = source[1] & 0xFF; int source_3 = source[2] & 0xFF; int source_4 = source[3] & 0xFF; return ((source_1 << 24) + (source_2 << 16) + (source_3 << 8) + (source_4 << 0)); } | cs |
<< byte * 4 -> int >> (byte 4개 조립)
1 2 3 4 5 6 | public int byteArray2Int(byte src_1,byte src_2,byte src_3, byte src_4) { byte[] temp_byte_array = new byte[]{src_1, src_2, src_3, byte src_4}; return java.nio.ByteBuffer.wrap(temp_byte_array).getInt(); } | cs |
<< String -> byte (Array) >>
1 2 3 4 | String string_1 = “Hello World!”; // 변수 string_1의 바이트 값 (아스키 코드) byte[] byteBuffer = string_1.getBytes(); | cs |
<< byte (Array) -> String >> (1)
1 2 3 4 5 6 | // 바이트 배열 자체의 문자열 값 // [B@ca0b6 String buffersToString = byteBuffer.toString(); // 바이트 배열을 문자열로 변환한 값 // Hello World! String string_2 = new String(byteBuffer); | cs |
<< byte (Array) -> String >> (2)
1 2 3 4 5 6 7 8 9 10 11 | public String byteArray2String(byte[] data) { if (data != null && data.length > 0) { final StringBuilder stringBuilder = new StringBuilder(data.length); for (byte byteChar : data) stringBuilder.append(String.format("%02X ", byteChar)); return stringBuilder.toString(); } return null; } | cs |
(출처 : 안드로이드 Develop 사이트에서 제공하는 BluetoothGatt 오픈 소스)
(위 코드 보다 더 좋은 방법이 있음......)
<< List<Integer> -> int (Array) >>
1 2 3 4 5 6 7 8 9 | public int[] convertIntegers(List<Integer> integers) { int[] ret = new int[integers.size()]; for (int i=0; i < ret.length; i++) { ret[i] = integers.get(i).intValue(); } return ret; } | cs |
1 2 3 4 5 6 7 8 9 10 | public static int[] convertIntegers(List<Integer> integers) { int[] ret = new int[integers.size()]; Iterator<Integer> iterator = integers.iterator(); for (int i = 0; i < ret.length; i++) { ret[i] = iterator.next().intValue(); } return ret; } | cs |
<< Double value 소수점 맞추기 >>
1 2 3 4 | DecimalFormat rateFormat = new DecimalFormat("0.00"); rateFormat.setRoundingMode(RoundingMode.HALF_EVEN); Double dv = 1.145; rateFormat.format(dv); | cs |
출처 : http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java
<< 숫자 자리수 따오기 >>
<< '초'를 '일','시','분','초'로 분리 >>
1 2 3 4 5 6 7 8 | public static void calculateTime(long seconds) { int day = (int) TimeUnit.SECONDS.toDays(seconds); long hours = TimeUnit.SECONDS.toHours(seconds) - (day * 24); long minute = TimeUnit.SECONDS.toMinutes(seconds) - (TimeUnit.SECONDS.toHours(seconds) * 60); long second = TimeUnit.SECONDS.toSeconds(seconds) - (TimeUnit.SECONDS.toMinutes(seconds) * 60); Log.d("test", "Day " + day + " Hour " + hours + " Minute " + minute + " Seconds " + second); } | cs |
http://stackoverflow.com/q/11357945/7017299
<String 에서 null 과 length 0의 차이?>
출처 : https://stackoverflow.com/a/45794912/7017299
'Study > Java, Android' 카테고리의 다른 글
[링크스크랩] Manage (or Hide) Api Key / 안드로이드 API 키 관리 (1) | 2019.06.13 |
---|---|
[Gradle] Android build variants (1) | 2019.05.10 |
Android Theme.MaterialComponents의 button 문제 (0) | 2019.04.10 |
안드로이드 스튜디오 단축키 (0) | 2019.02.12 |
Uri를 Intent로 전달 할 때... (0) | 2018.11.13 |