Java Long to IP Address longToIp(long ip)

Here you can find the source of longToIp(long ip)

Description

covert the ip address from decimal to dot formatted

License

Apache License

Parameter

Parameter Description
ip ip address decimal format;

Return

ip address in dot format.

Declaration

public static String longToIp(long ip) 

Method Source Code

//package com.java2s;
/*//from   w w  w .  j a v  a 2  s  . c om
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

public class Main {
    /**
     * covert the ip address from decimal to dot formatted
     *
     * @param ip ip address decimal format;
     * @return ip address in dot format.
     */
    public static String longToIp(long ip) {
        StringBuilder sb = new StringBuilder(15);
        for (int i = 0; i < 4; i++) {
            // 1. 2
            // 2. 1
            // 3. 168
            // 4. 192
            sb.insert(0, Long.toString(ip & 0xff));

            if (i < 3) {
                sb.insert(0, '.');
            }

            // 1. 192.168.1.2
            // 2. 192.168.1
            // 3. 192.168
            // 4. 192
            ip = ip >> 8;

        }

        return sb.toString();
    }
}

Related

  1. long2IpAdress(long src)
  2. longToIp(long address)
  3. longToIp(long i)
  4. longToIp(long i)
  5. longToIp(long ip)
  6. LongToIp(long longIp)
  7. longToIp(long longValue)
  8. longToIp(String ip)
  9. longToIpAddress(long ip)