Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.UnsupportedEncodingException;

public class Main {
    public static String hexStringToString(String input, String encoding) throws UnsupportedEncodingException {
        return input != null ? new String(fromHex(input), encoding) : null;
    }

    public static String hexStringToString(String input) {
        try {
            return hexStringToString(input, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException("UTF-8 encoding is not supported by JVM");
        }
    }

    public static byte[] fromHex(String input) {
        if (input == null)
            return null;
        byte output[] = new byte[input.length() / 2];
        for (int i = 0; i < output.length; i++)
            output[i] = (byte) Integer.parseInt(input.substring(i * 2, (i + 1) * 2), 16);

        return output;
    }
}