Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    public static String bytesToIpPortString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            int Ip;
            if (bytes[i] < 0) {
                Ip = bytes[i] + 256;
            } else {
                Ip = bytes[i];
            }
            sb.append(String.valueOf(Ip));
            if (i < 3) {
                sb.append(".");
            } else {
                sb.append(":");
            }
        }
        int portHigh = (bytes[5] >= 0 ? bytes[5] : bytes[5] + 256);
        int portLow = (bytes[4] >= 0 ? bytes[4] : bytes[4] + 256);
        int port = portHigh * 256 + portLow;
        sb.append(String.valueOf(port));
        return sb.toString();
    }
}