I have a byte array filled with hex numbers and printing it the easy way is pretty pointless because there are many unprintable elements. What I need is the exact hexcode in the form of: 3a5f771c
Java – How to convert a byte array to a hex string in Java
bytearrayhexjava
Related Topic
- Java – How to read / convert an InputStream into a String in Java
- C# – How to convert a byte array to a hexadecimal string, and vice versa
- Java – How to generate random integers within a specific range in Java
- Java – How to declare and initialize an array in Java
- Java – Convert InputStream to byte array in Java
- Java – How to convert a String to an int in Java
- Java – How to create a memory leak in Java
Best Answer
From the discussion here, and especially this answer, this is the function I currently use:
My own tiny benchmarks (a million bytes a thousand times, 256 bytes 10 million times) showed it to be much faster than any other alternative, about half the time on long arrays. Compared to the answer I took it from, switching to bitwise ops --- as suggested in the discussion --- cut about 20% off of the time for long arrays. (Edit: When I say it's faster than the alternatives, I mean the alternative code offered in the discussions. Performance is equivalent to Commons Codec, which uses very similar code.)
2k20 version, with respect to Java 9 compact strings: