Feb 28

A lopp can be used to display all through all the characters and the isDefined() method of the Character class can be used to determine if a given character is a valid Unicode character or not

for (int i=0; i<=Integer.MAX_VALUE; i++)
{
    if (Character.isDefined(i))
    {
         System.out.println(Integer.toHexString(i)+": "+
             new String(Character.toChars(i)));
    }
}

written by objects \\ tags: , ,

Nov 23

You can tell the parseInt() method what base number system is used by the String that is being parsed.


String hex = "a9b0";
int n = Integer.parseInt(hex, 16);

written by objects \\ tags: , ,

Oct 13

You have a couple of choices, either:

  • Loop thru the array to convert each byte individually, or
  • Use the toString() method of the BigInteger class

// using a loop

StringBuilder sb = 
   new StringBuilder(bytes.length * 2);
for (int i=0; i< bytes.length; i++)
{
    sb.append(String.format("%02x", bytes[i]));
}
String hex1 = sb.toString();

// using BigInteger

BigInteger bi = new BigInteger(bytes);
String hex2 = bi.toString(16); 

If you are dealing with large byte arrays or performance is an issue then you should have a read of “Converting large byte array to binary string”.

written by objects \\ tags: , , , ,