package kr.kangwoo.util; /** * ÀÌ Å¬·¡½º´Â Byte °ü·Ã ÇÔ¼ö¸¦ Á¦°øÇÕ´Ï´Ù. * * @author kangwoo * @version 1.0 * @since 1.0 */ public class ByteUtils { public static Byte DEFAULT_BYTE = new Byte((byte) 0); /** *

¹®ÀÚ¿­À» ¹ÙÀÌÆ®·Î º¯È¯ÇÑ´Ù.

* *
	 * ByteUtils.toByte("1", *)    = 0x01
	 * ByteUtils.toByte("-1", *)   = 0xff
	 * ByteUtils.toByte("a", 0x00) = 0x00
	 * 
* * @param value 10Áø¼ö ¹®ÀÚ¿­ °ª * @param defaultValue * @return */ public static byte toByte(String value, byte defaultValue) { try { return Byte.parseByte(value); } catch(Exception e) { return defaultValue; } } /** *

¹®ÀÚ¿­À» ¹ÙÀÌÆ®·Î º¯È¯ÇÑ´Ù.

* *
	 * ByteUtils.toByteObject("1", *)    = 0x01
	 * ByteUtils.toByteObject("-1", *)   = 0xff
	 * ByteUtils.toByteObject("a", 0x00) = 0x00
	 * 
* * @param value 10Áø¼ö ¹®ÀÚ¿­ °ª * @param defaultValue * @return */ public static Byte toByteObject(String value, Byte defaultValue) { try { return new Byte(value); } catch (Exception e) { return defaultValue; } } /** *

singed byte¸¦ unsinged byte·Î º¯È¯ÇÑ´Ù.

*

Java¿¡´Â unsinged ŸÀÔÀÌ ¾ø±â¶§¹®¿¡, int·Î ¹ÝȯÇÑ´Ù.(b & 0xff)

* * @param b singed byte * @return unsinged byte */ public static int unsignedByte(byte b) { return b & 0xFF; } /** *

ÀÔ·ÂÇÑ ¹ÙÀÌÆ® ¹è¿­(4¹ÙÀÌÆ®)À» int ÇüÀ¸·Î º¯È¯ÇÑ´Ù.

* * @param src * @param srcPos * @return */ public static int toInt(byte[] src, int srcPos) { int dword = 0; for (int i = 0; i < 4; i++) { dword = (dword << 8) + (src[i + srcPos] & 0xFF); } return dword; } /** *

ÀÔ·ÂÇÑ ¹ÙÀÌÆ® ¹è¿­(4¹ÙÀÌÆ®)À» int ÇüÀ¸·Î º¯È¯ÇÑ´Ù.

* * @param src * @return */ public static int toInt(byte[] src) { return toInt(src, 0); } /** *

ÀÔ·ÂÇÑ ¹ÙÀÌÆ® ¹è¿­(8¹ÙÀÌÆ®)À» long ÇüÀ¸·Î º¯È¯ÇÑ´Ù.

* * @param src * @param srcPos * @return */ public static long toLong(byte[] src, int srcPos) { long qword = 0; for (int i = 0; i < 8; i++) { qword = (qword << 8) + (src[i + srcPos] & 0xFF); } return qword; } /** *

ÀÔ·ÂÇÑ ¹ÙÀÌÆ® ¹è¿­(8¹ÙÀÌÆ®)À» long ÇüÀ¸·Î º¯È¯ÇÑ´Ù.

* * @param src * @return */ public static long toLong(byte[] src) { return toLong(src, 0); } /** *

int ÇüÀÇ °ªÀ» ¹ÙÀÌÆ® ¹è¿­(4¹ÙÀÌÆ®)·Î º¯È¯ÇÑ´Ù.

* * @param value * @param dest * @param destPos */ public static void toBytes(int value, byte[] dest, int destPos) { for (int i = 0; i < 4; i++) { dest[i + destPos] = (byte)(value >> ((7 - i) * 8)); } } /** *

int ÇüÀÇ °ªÀ» ¹ÙÀÌÆ® ¹è¿­(4¹ÙÀÌÆ®)·Î º¯È¯ÇÑ´Ù.

* * @param value * @return */ public static byte[] toBytes(int value) { byte[] dest = new byte[4]; toBytes(value, dest, 0); return dest; } /** *

long ÇüÀÇ °ªÀ» ¹ÙÀÌÆ® ¹è¿­(8¹ÙÀÌÆ®)·Î º¯È¯ÇÑ´Ù.

* * @param value * @param dest * @param destPos */ public static void toBytes(long value, byte[] dest, int destPos) { for (int i = 0; i < 8; i++) { dest[i + destPos] = (byte)(value >> ((7 - i) * 8)); } } /** *

long ÇüÀÇ °ªÀ» ¹ÙÀÌÆ® ¹è¿­(8¹ÙÀÌÆ®)·Î º¯È¯ÇÑ´Ù.

* * @param value * @return */ public static byte[] toBytes(long value) { byte[] dest = new byte[8]; toBytes(value, dest, 0); return dest; } /** *

8, 10, 16Áø¼ö ¹®ÀÚ¿­À» ¹ÙÀÌÆ® ¹è¿­·Î º¯È¯ÇÑ´Ù.

*

8, 10Áø¼öÀÎ °æ¿ì´Â ¹®ÀÚ¿­ÀÇ 3ÀÚ¸®°¡, 16Áø¼öÀÎ °æ¿ì´Â 2ÀÚ¸®°¡, ÇϳªÀÇ byte·Î ¹Ù²ï´Ù.

* *
	 * ByteUtils.toBytes(null)     = null
	 * ByteUtils.toBytes("0E1F4E", 16) = [0x0e, 0xf4, 0x4e]
	 * ByteUtils.toBytes("48414e", 16) = [0x48, 0x41, 0x4e]
	 * 
* * @param digits ¹®ÀÚ¿­ * @param radix Áø¼ö(8, 10, 16¸¸ °¡´É) * @return * @throws NumberFormatException */ public static byte[] toBytes(String digits, int radix) throws IllegalArgumentException, NumberFormatException { if (digits == null) { return null; } if (radix != 16 && radix != 10 && radix != 8) { throw new IllegalArgumentException("For input radix: \"" + radix + "\""); } int divLen = (radix == 16) ? 2 : 3; int length = digits.length(); if (length % divLen == 1) { throw new IllegalArgumentException("For input string: \"" + digits + "\""); } length = length / divLen; byte[] bytes = new byte[length]; for (int i = 0; i < length; i++) { int index = i * divLen; bytes[i] = (byte)(Short.parseShort(digits.substring(index, index+divLen), radix)); } return bytes; } /** *

16Áø¼ö ¹®ÀÚ¿­À» ¹ÙÀÌÆ® ¹è¿­·Î º¯È¯ÇÑ´Ù.

*

¹®ÀÚ¿­ÀÇ 2ÀÚ¸®°¡ ÇϳªÀÇ byte·Î ¹Ù²ï´Ù.

* *
	 * ByteUtils.toBytesFromHexString(null)     = null
	 * ByteUtils.toBytesFromHexString("0E1F4E") = [0x0e, 0xf4, 0x4e]
	 * ByteUtils.toBytesFromHexString("48414e") = [0x48, 0x41, 0x4e]
	 * 
* * @param digits 16Áø¼ö ¹®ÀÚ¿­ * @return * @throws NumberFormatException * @see HexUtils.toBytes(String) */ public static byte[] toBytesFromHexString(String digits) throws IllegalArgumentException, NumberFormatException { if (digits == null) { return null; } int length = digits.length(); if (length % 2 == 1) { throw new IllegalArgumentException("For input string: \"" + digits + "\""); } length = length / 2; byte[] bytes = new byte[length]; for (int i = 0; i < length; i++) { int index = i * 2; bytes[i] = (byte)(Short.parseShort(digits.substring(index, index+2), 16)); } return bytes; } /** *

unsigned byte(¹ÙÀÌÆ®)¸¦ 16Áø¼ö ¹®ÀÚ¿­·Î ¹Ù²Û´Ù.

* * ByteUtils.toHexString((byte)1) = "01" * ByteUtils.toHexString((byte)255) = "ff" * * @param b unsigned byte * @return * @see HexUtils.toString(byte) */ public static String toHexString(byte b) { StringBuffer result = new StringBuffer(3); result.append(Integer.toString((b & 0xF0) >> 4, 16)); result.append(Integer.toString(b & 0x0F, 16)); return result.toString(); } /** *

unsigned byte(¹ÙÀÌÆ®) ¹è¿­À» 16Áø¼ö ¹®ÀÚ¿­·Î ¹Ù²Û´Ù.

* *
	 * ByteUtils.toHexString(null)                   = null
	 * ByteUtils.toHexString([(byte)1, (byte)255])   = "01ff"
	 * 
* * @param bytes unsigned byte's array * @return * @see HexUtils.toString(byte[]) */ public static String toHexString(byte[] bytes) { if (bytes == null) { return null; } StringBuffer result = new StringBuffer(); for (byte b : bytes) { result.append(Integer.toString((b & 0xF0) >> 4, 16)); result.append(Integer.toString(b & 0x0F, 16)); } return result.toString(); } /** *

unsigned byte(¹ÙÀÌÆ®) ¹è¿­À» 16Áø¼ö ¹®ÀÚ¿­·Î ¹Ù²Û´Ù.

* *
	 * ByteUtils.toHexString(null, *, *)                   = null
	 * ByteUtils.toHexString([(byte)1, (byte)255], 0, 2)   = "01ff"
	 * ByteUtils.toHexString([(byte)1, (byte)255], 0, 1)   = "01"
	 * ByteUtils.toHexString([(byte)1, (byte)255], 1, 2)   = "ff"
	 * 
* * @param bytes unsigned byte's array * @return * @see HexUtils.toString(byte[]) */ public static String toHexString(byte[] bytes, int offset, int length) { if (bytes == null) { return null; } StringBuffer result = new StringBuffer(); for (int i = offset; i < offset + length; i++) { result.append(Integer.toString((bytes[i] & 0xF0) >> 4, 16)); result.append(Integer.toString(bytes[i] & 0x0F, 16)); } return result.toString(); } /** *

µÎ ¹è¿­ÀÇ °ªÀÌ µ¿ÀÏÇÑÁö ºñ±³ÇÑ´Ù.

* *
	 * ArrayUtils.equals(null, null)                        = true
	 * ArrayUtils.equals(["one", "two"], ["one", "two"])    = true
	 * ArrayUtils.equals(["one", "two"], ["three", "four"]) = false
	 * 
* * @param array1 * @param array2 * @return µ¿ÀÏÇϸé true, ¾Æ´Ï¸é false */ public static boolean equals(byte[] array1, byte[] array2) { if (array1 == array2) { return true; } if (array1 == null || array2 == null) { return false; } if (array1.length != array2.length) { return false; } for (int i = 0; i < array1.length; i++) { if (array1[i] != array2[i]) { return false; } } return true; } }