36

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?

Ram Dutt Shukla
  • 1,351
  • 7
  • 28
  • 55
hiii
  • 571
  • 2
  • 6
  • 10

8 Answers8

55
public static String bytesToHex(byte[] in) {
    final StringBuilder builder = new StringBuilder();
    for(byte b : in) {
        builder.append(String.format("%02x", b));
    }
    return builder.toString();
}
Vasyl Keretsman
  • 2,688
  • 2
  • 18
  • 15
48

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

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
4

How about:

public static String bytesToHex(byte[] bytes) {
    final char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
    char[] hexChars = new char[bytes.length * 2];
    int v;
    for ( int j = 0; j < bytes.length; j++ ) {
        v = bytes[j] & 0xFF;
        hexChars[j * 2] = hexArray[v >>> 4];
        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
    }
    return new String(hexChars);
}

Source

Community
  • 1
  • 1
Burkhard
  • 14,596
  • 22
  • 87
  • 108
1

This method should do that for you..pass in the byte array as a parameter to return the hex string...

private static String convertToHexString(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
    int halfbyte = (data[i] >>> 4) & 0x0F;
    int two_halfs = 0;
    do {
        if ((0 <= halfbyte) && (halfbyte <= 9))
            buf.append((char) ('0' + halfbyte));
        else
            buf.append((char) ('a' + (halfbyte - 10)));
            halfbyte = data[i] & 0x0F;
        } while(two_halfs++ < 1);
    }
return buf.toString();
}

hope that helps..

Lucky
  • 16,787
  • 19
  • 117
  • 151
0

You could use Integer.toStringint i, int radix) to convert each of your numbers into a string in hexadecimal, and then concatenate them together. Now you just have to get your bytes into an int.

Miquel
  • 15,405
  • 8
  • 54
  • 87
0

Refer to Integer's toHexString. Converting your byte to int and then obtaining the resulting String should do the trick. Of course, you would have to concatenate the Strings with a StringBuilder then.

afsantos
  • 5,178
  • 4
  • 30
  • 54
0

If this array is 4 bytes long, you could probably concatenate bytes and make use of Integer.toString(int, int) method.

byte[] array = /* initialization */;
int x = 0;
for (int i = 0, l = Math.min(array.length, 4); i < l; i++) {
    x <<= 8;
    x |= (array[i] & 0xFF);
}
String hex = Integer.toString(x, 16);
St.Shadow
  • 1,840
  • 1
  • 12
  • 16
Cromax
  • 1,822
  • 1
  • 23
  • 35
0
import java.util.HashMap;

public class NumUtil
{   private static HashMap<Byte,char[]>byteToHex=new HashMap<Byte, char[]>();
    static
    {   for(int i=0;i<16;++i)
            byteToHex.put((byte)i, new char[]{'0',Integer.toHexString(i).charAt(0)});
        for(int i=16;i<256;++i)
            byteToHex.put((byte)i, Integer.toHexString(i).toCharArray());
    }
    public static String toHexString(byte[]bytes)
    {   StringBuilder stringBuilder = new StringBuilder(bytes.length*2);
        for(byte b:bytes)
            stringBuilder.append(byteToHex.get(b));
        return stringBuilder.toString();
    }
}
Abhishek Oza
  • 3,340
  • 1
  • 27
  • 35