-//
-// Copyright (c) 2013 Ford Motor Company
-//
-package com.smartdevicelink.util;
-
-public class BitConverter {
- public static String bytesToHex(byte [] bytes) {
- return bytesToHex(bytes, 0, bytes.length);
- } // end-method
-
- public static String bytesToHex(byte[] bytes, int offset, int length) {
- if (bytes == null) { return null; }
- final char[] HexDigits = new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
- byte b;
- char[] hexChars = new char[2 * length];
- //StringBuffer sb = new StringBuffer();
- int upperBound = Math.min(bytes.length, (offset + length));
- int baidx = 0;
- int sidx = 0;
- for (baidx = offset; baidx < upperBound; baidx++) {
- // Get the byte from the array
- b = bytes[baidx];
- // Use nibbles as index into hex digit array (left nibble, then right)
- hexChars[sidx++] = HexDigits[(b & 0xf0) >> 4];
- hexChars[sidx++] = HexDigits[(b & 0x0f)];
- } // end-for
- return new String(hexChars);
- } // end-method
-
- public static byte [] hexToBytes(String hexString) {
- if (hexString == null) { return null; }
- if (hexString.length() % 2 != 0) {
- hexString = "0" + hexString;
- }
- byte [] theBytes = new byte[hexString.length() / 2];
- for (int i = 0; i < hexString.length(); i += 2) {
- String byteString = hexString.substring(i, i + 2);
- byte theByte = (byte)Integer.parseInt(byteString, 16);
- theBytes[i/2] = theByte;
- }
- return theBytes;
- } // end-method
-
- public static final byte[] intToByteArray(int value) {
- return new byte[] {
- (byte)(value >>> 24),
- (byte)(value >>> 16),
- (byte)(value >>> 8),
- (byte)value};
- }
-
- public static int intFromByteArray(byte[] sizeBuf, int offset) {
- int ret = 0;
- for (int i = offset; i < offset + 4; i++) {
- ret <<= 8;
- ret |= 0xFF & sizeBuf[i];
- }
- return ret;
- }
-
- public static final byte[] shortToByteArray(short value) {
- return new byte[] {
- (byte)(value >>> 8),
- (byte)value};
- }
-
- public static short shortFromByteArray(byte[] sizeBuf, int offset) {
- short ret = 0;
- for (int i = offset; i < offset + 2; i++) {
- ret <<= 8;
- ret |= 0xFF & sizeBuf[i];
- }
- return ret;
- }
-}
+package com.smartdevicelink.util;\r
+\r
+public class BitConverter {\r
+ public static String bytesToHex(byte [] bytes) {\r
+ return bytesToHex(bytes, 0, bytes.length);\r
+ } // end-method\r
+\r
+ public static String bytesToHex(byte[] bytes, int offset, int length) {\r
+ if (bytes == null) { return null; }\r
+ final char[] HexDigits = new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};\r
+ byte b;\r
+ char[] hexChars = new char[2 * length];\r
+ //StringBuffer sb = new StringBuffer();\r
+ int upperBound = Math.min(bytes.length, (offset + length));\r
+ int baidx = 0;\r
+ int sidx = 0;\r
+ for (baidx = offset; baidx < upperBound; baidx++) {\r
+ // Get the byte from the array\r
+ b = bytes[baidx];\r
+ // Use nibbles as index into hex digit array (left nibble, then right)\r
+ hexChars[sidx++] = HexDigits[(b & 0xf0) >> 4];\r
+ hexChars[sidx++] = HexDigits[(b & 0x0f)];\r
+ } // end-for\r
+ return new String(hexChars);\r
+ } // end-method\r
+\r
+ public static byte [] hexToBytes(String hexString) {\r
+ if (hexString == null) { return null; }\r
+ if (hexString.length() % 2 != 0) {\r
+ hexString = "0" + hexString;\r
+ }\r
+ byte [] theBytes = new byte[hexString.length() / 2];\r
+ for (int i = 0; i < hexString.length(); i += 2) {\r
+ String byteString = hexString.substring(i, i + 2);\r
+ byte theByte = (byte)Integer.parseInt(byteString, 16);\r
+ theBytes[i/2] = theByte;\r
+ }\r
+ return theBytes;\r
+ } // end-method\r
+\r
+ public static final byte[] intToByteArray(int value) {\r
+ return new byte[] {\r
+ (byte)(value >>> 24),\r
+ (byte)(value >>> 16),\r
+ (byte)(value >>> 8),\r
+ (byte)value};\r
+ }\r
+ \r
+ public static int intFromByteArray(byte[] sizeBuf, int offset) {\r
+ int ret = 0;\r
+ for (int i = offset; i < offset + 4; i++) {\r
+ ret <<= 8;\r
+ ret |= 0xFF & sizeBuf[i];\r
+ }\r
+ return ret;\r
+ }\r
+\r
+ public static final byte[] shortToByteArray(short value) {\r
+ return new byte[] {\r
+ (byte)(value >>> 8),\r
+ (byte)value};\r
+ }\r
+ \r
+ public static short shortFromByteArray(byte[] sizeBuf, int offset) {\r
+ short ret = 0;\r
+ for (int i = offset; i < offset + 2; i++) {\r
+ ret <<= 8;\r
+ ret |= 0xFF & sizeBuf[i];\r
+ }\r
+ return ret;\r
+ }\r
+}\r