diff --git a/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java b/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java index df6b87bfe8..db36db9b73 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java @@ -1255,6 +1255,22 @@ public final class Util { return data; } + /** + * Returns a string containing a lower-case hex representation of the bytes provided. + * + * @param bytes The byte data to convert to hex. + * @return A String containing the hex representation of {@code bytes}. + */ + public static String toHexString(byte[] bytes) { + StringBuilder result = new StringBuilder(bytes.length * 2); + for (int i = 0; i < bytes.length; i++) { + result + .append(Character.forDigit((bytes[i] >> 4) & 0xF, 16)) + .append(Character.forDigit(bytes[i] & 0xF, 16)); + } + return result.toString(); + } + /** * Returns a string with comma delimited simple names of each object's class. * diff --git a/library/common/src/test/java/com/google/android/exoplayer2/util/UtilTest.java b/library/common/src/test/java/com/google/android/exoplayer2/util/UtilTest.java index 2dc4d2b8fc..88835a40a6 100644 --- a/library/common/src/test/java/com/google/android/exoplayer2/util/UtilTest.java +++ b/library/common/src/test/java/com/google/android/exoplayer2/util/UtilTest.java @@ -705,6 +705,13 @@ public class UtilTest { assertThat(Util.toLong(0xFEDCBA, 0x87654321)).isEqualTo(0xFEDCBA_87654321L); } + @Test + public void testToHexString() { + byte[] bytes = TestUtil.createByteArray(0x12, 0xFC, 0x06); + + assertThat(Util.toHexString(bytes)).isEqualTo("12fc06"); + } + @Test public void testGetCodecsOfType() { assertThat(getCodecsOfType(null, C.TRACK_TYPE_VIDEO)).isNull();