Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Locale;

public class Main {
    private static void test_getPortForMesh() {
        int port1 = 7777;
        String result1 = getPortForMesh(port1);

        int port2 = 10;
        String result2 = getPortForMesh(port2);

        if (result1.equals("1E61") && result2.equals("000A")) {
            System.out.println("test_getPortForMesh() pass");
        } else {
            System.out.println("test_getPortForMesh() fail");
        }
    }

    /**
     * Get the port for mesh usage(For mesh require port hex uppercase).
     * 
     * @param port the port
     * @return the port for mesh usage
     */
    public static String getPortForMesh(int port) {
        String portHexUppercase = Integer.toHexString(port).toUpperCase(Locale.US);
        int numberOfZero = 4 - portHexUppercase.length();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < numberOfZero; i++) {
            sb.append("0");
        }
        sb.append(portHexUppercase);
        return sb.toString();
    }
}