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 {
    /**
     * Get the ip address for mesh usage(For mesh require the ip address hex uppercase without ".".
     * 
     * @param hostname the ip address, e.g. 192.168.1.2
     * @return ip address by hex without ".", e.g. C0A80102
     */
    public static String getIpAddressForMesh(String hostname) {
        StringBuilder sb = new StringBuilder();
        String[] segments = hostname.split("\\.");
        int segment;
        String segmentHexStr;
        for (int i = 0; i < segments.length; i++) {
            // get the integer
            segment = Integer.parseInt(segments[i]);
            // transform the integer to hex
            segmentHexStr = Integer.toHexString(segment);
            // transform the hex string to uppercase
            segmentHexStr = segmentHexStr.toUpperCase(Locale.US);
            // append segmentHexStr to the sb
            if (segmentHexStr.length() == 1) {
                sb.append("0");
                sb.append(segmentHexStr);
            } else if (segmentHexStr.length() == 2) {
                sb.append(segmentHexStr);
            } else {
                throw new RuntimeException();
            }
        }
        return sb.toString();
    }
}