}
- /**
- * Convert string to byte
- *
- * ByteUtils.toByte("1", *) = 0x01 ByteUtils.toByte("-1", *) = 0xff
- *
- * @param value
- * @param defaultValue
- * @return
- */
- @Deprecated
- public static byte toByte(String value, byte defaultValue) {
- try {
- return Byte.parseByte(value);
- } catch (Exception e) {
- return defaultValue;
- }
- }
-
- /**
- * Convert the string that formatted Octal or decimal or hexadecimal to byte array.
- * Case of octal and Decimal, that change to three-digit string.
- * Case of hexadecimal, that change to one byte.
- * *
- * ByteUtils.toBytes(null) = null ByteUtils.toBytes("0E1F4E", 16) = [0x0e,
- * 0xf4, 0x4e] ByteUtils.toBytes("48414e", 16) = [0x48, 0x41, 0x4e]
- *
- * @param digits
- * string of number
- * @param radix
- * base(can be 8, 10, 16)
- * @return
- * @throws NumberFormatException
- */
- @Deprecated
- 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;
- }
-
- /**
- * Convert hexadecimal formatted string to byte array/
- * The two-digit string change to one byte.
- *
- * ByteUtils.hexStringToBytes(null) = null
- * ByteUtils.hexStringToBytes("0E1F4E") = [0x0e, 0xf4, 0x4e]
- * ByteUtils.hexStringToBytes("48414e") = [0x48, 0x41, 0x4e]
- *
- * @param digits
- * string of base-16 number
- * @return
- * @throws NumberFormatException
- */
- @Deprecated
- public static byte[] hexStringToBytes(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;
- }
}