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 stringToHexString(String input, String encoding) throws UnsupportedEncodingException {
        return input != null ? toHex(input.getBytes(encoding)) : null;
    }

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

    public static String toHex(byte input[]) {
        if (input == null)
            return null;
        StringBuffer output = new StringBuffer(input.length * 2);
        for (int i = 0; i < input.length; i++) {
            int current = input[i] & 0xff;
            if (current < 16)
                output.append("0");
            output.append(Integer.toString(current, 16));
        }

        return output.toString();
    }
}