I have byte array that consist of hex values like CA ,FA,21,33
But I want to list them in JList
as a single element CAFA2133
. In order to list them in JList I think I need to convert it to string
. So any recommendation?
Double is a higher datatype compared to byte. Therefore, double value will not be converted into byte implicitly, you need to convert it using the cast operator.
There are two ways to convert byte array to String: By using String class constructor. By using UTF-8 encoding.
The BigInteger class has a longValue() method to convert a byte array to a long value: long value = new BigInteger(bytes). longValue();
public static String bytesToHex(byte[] in) { final StringBuilder builder = new StringBuilder(); for(byte b : in) { builder.append(String.format("%02x", b)); } return builder.toString(); }
You need to look at String.format() and the Formatter specifications.
e.g.
String.format("%02x", byteValue);
Iterate through the array and append each String.format()
result to a StringBuilder
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With