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