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?

0

8 Answers 8

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();
}
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

Nice and simple. No need for me to sift through the wall of text on the other duplicate question and answers :)
it works, but it takes about 3 times longer than the method below based on byte operations
Interesting. Can you link to the specific answer below ?
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

3 Comments

Looks like copy and paste from the duplicate question. You should at least mention the source from where you copied it.
he does have the source link as a part of the answer
@MrSmith42: I've put the source in bold for you ;)
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..

Comments

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.

Comments

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.

Comments

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();
    }
}

Comments

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.