I asked a week ago a question about this, now I tried to google around and found this toHex method. It has an empty spot about the charset. For example I want to use UTF-8 but if I enter it returns an error and if I don't specify it, the output is huge amount of 0s and few numbers afterwards. My goal is a Hex representation of the entered string

public String toHex(String arg) {
    return String.format("%040x", new BigInteger(1, arg.getBytes(/*YOUR_CHARSET?*/)));
    } 
    public static void main(String[] args){
        EncodingHex ec = new EncodingHex();

        System.out.println(ec.toHex("abv"));
    }

Ahhh - the old BigInteger trick. What you have found is an over-complicated version. The simplest form is just one short line...

You can use the simpler version of getBytes without any arguments - that just uses the default char set. If you stick to ASCII characters you'll be OK.

Then you can use BigInteger's toString(int radix) method with a radix of 16 to get a hex string, no need for any other formatting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.