Strings { public static String byteToUnsignedHexString(byte b) { int value = b; value &= 0xFF; return ( value < 16 ? "0x0" + toUpperCase(Integer.toHexString(value)) : "0x" + toUpperCase(Integer.toHexString(value))); } private  Strings(); static String byteToUnsignedHexString(byte b); static String bytesToHex(byte[] bytes); static String toUpperCase(String str); static String toLowerCase(String str); static int convertPollingIntervalString(String pollingInterval);  }
@Test public void testSingleDigits() { byte[] digits = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; for (int i = 0; i < digits.length; ++i) { String str = Strings.byteToUnsignedHexString(digits[i]); String expected = "0x0" + (i+1); Assert.assertTrue( "Expecting '" + expected + "', got '" + str + "'.", str.equals("0x0" + (i + 1)) ); } }
@Test public void testHex() { byte A = 10; byte B = 11; byte C = 12; byte D = 13; byte E = 14; byte F = 15; String strA = Strings.byteToUnsignedHexString(A); String strB = Strings.byteToUnsignedHexString(B); String strC = Strings.byteToUnsignedHexString(C); String strD = Strings.byteToUnsignedHexString(D); String strE = Strings.byteToUnsignedHexString(E); String strF = Strings.byteToUnsignedHexString(F); Assert.assertTrue(strA.equals("0x0A")); Assert.assertTrue(strB.equals("0x0B")); Assert.assertTrue(strC.equals("0x0C")); Assert.assertTrue(strD.equals("0x0D")); Assert.assertTrue(strE.equals("0x0E")); Assert.assertTrue(strF.equals("0x0F")); }
@Test public void testDoubleDigits() { byte a10 = 0x10; byte a20 = 0x20; byte a35 = 0x35; byte a4A = 0x4A; byte a5F = 0x5F; String str10 = Strings.byteToUnsignedHexString(a10); String str20 = Strings.byteToUnsignedHexString(a20); String str35 = Strings.byteToUnsignedHexString(a35); String str4A = Strings.byteToUnsignedHexString(a4A); String str5F = Strings.byteToUnsignedHexString(a5F); Assert.assertTrue(str10.equals("0x10")); Assert.assertTrue(str20.equals("0x20")); Assert.assertTrue(str35.equals("0x35")); Assert.assertTrue(str4A.equals("0x4A")); Assert.assertTrue(str5F.equals("0x5F")); }
@Test public void testHighBits() { byte a_1 = -1; byte a_2 = -2; byte a_128 = -128; byte a_127 = -127; String str_1 = Strings.byteToUnsignedHexString(a_1); String str_2 = Strings.byteToUnsignedHexString(a_2); String str_128 = Strings.byteToUnsignedHexString(a_128); String str_127 = Strings.byteToUnsignedHexString(a_127); Assert.assertTrue( "Expecting '0xFF', got '" + str_1 + "'.", str_1.equals("0xFF") ); Assert.assertTrue( "Expecting '0xFE', got '" + str_2 + "'.", str_2.equals("0xFE") ); Assert.assertTrue( "Expecting '0x80', got '" + str_128 + "'.", str_128.equals("0x80") ); Assert.assertTrue( "Expecting '0x81', got '" + str_127 + "'.", str_127.equals("0x81") ); }
@Test public void testZero() { byte zero = 0; Assert.assertTrue(Strings.byteToUnsignedHexString(zero).equals("0x00")); }