IP address, string, number format conversion

Source: Internet
Author: User
Tags error handling stringbuffer

Common tools, no more introduction, on the code.

Converts the IP address in the 127.0.0.1 form to a decimal integer, where no error handling is done public static long Iptolong (String strIp) {long[] IP = new LONG[4];//Find the IP address word first The position of the string. int position1 = Strip.indexof ("."); int position2 = Strip.indexof (".", Position1 + 1); int position3 = Strip.indexof (".", Position2 + 1); Converts the string between each. To integral type ip[0] = Long.parselong (strip.substring (0, Position1)); IP[1] = Long.parselong (strip.substring (position1 + 1, position2)); IP[2] = Long.parselong (strip.substring (position2 + 1, position3)); IP[3] = Long.parselong (strip.substring (Position3 + 1)); Return (Ip[0] <<) + (Ip[1] <<) + (Ip[2] << 8) + ip[3]; Convert decimal integer form to 127.0.0.1 IP address public static String Longtoip (long Longip) {StringBuffer sb = new StringBuffer (""); Move directly to the right 24-bit sb.append (string.valueof (LONGIP >>> 24)); Sb.append ("."); will be high 8 position 0, then the right to move 16-bit sb.append (string.valueof (LONGIP & 0x00ffffff) >>> 16)); Sb.append ("."); will be high 16 position 0, then the right to move 8-bit sb.append (string.valueof (LONGIP & 0x0000ffff) >>> 8));Sb.append ("."); will be high 24 position 0 sb.append (string.valueof (LONGIP & 0x000000ff)); return sb.tostring (); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.