Given an IP address, it is converted to binary 32, and then converted to a decimal, a method of writing into decimal IP address allowed

Decimal values ​​are known

the first method:

<script type="text/javascript">

NUM = 2,148,140,545 var;
var = num.toString STR ( "2"); // Switch to binary
var str1 = parseInt (str.substring (0,8 ), 2);


/ * parseInt function for parsing a string, and returns an integer
substring () method for extracting specified character string intermediary between the two subscripts.
stringObject.substring (Start, STOP)
Start required. A non-negative integer, a predetermined position of the first character of the substring to be extracted in the stringObject.
Optional stop. A non-negative integer, to extract the last character of the substring than a position in a multi stringObject.
If omitted, then the substring will always return to the end of the string.
* /


var str2=parseInt(str.substring(8,16),2);
var str3=parseInt(str.substring(16,24),2);
var str4=parseInt(str.substring(24,str.length),2);
console.log(str1+"."+str2+"."+str3+"."+str4);
</script>

The second method:

<Script type = "text / JavaScript">

function INT2IP (NUM) {
var STR;
var = new new TT the Array ();
TT [0] = (NUM >>> 24) >>> 0;
TT [. 1] = ( (<< NUM. 8) >>> 24) >>> 0;
TT [2] = (NUM << 16) >>> 24;
TT [. 3] = (NUM << 24) >>> 24;
STR = . "" String (TT [0]) + + String (TT [. 1]) + + String (TT [2]) + + String (TT [. 3]). "". "";
return STR;
}
var Sun = int2iP (2148140545); // parameter passing
the console.log (Sun)
/ *
100 2 >>>
binary 100 is
01100100
to the right by 2 bits
00011001
final result 25
100 25 >>> 2 ==
unsigned displacement (>>>) and signed displacement (>>) Is that
when the shift operation after a signed displacement leading zero, is negative after the displacement of a front up if the number is positive
Example
100 2 >> calculation == 25 and above as
if 100 negative number
>>> -100 2
-100 to binary -_- long.
1111111111111111111111111111111111111111111111111111111111111100
remove the final two digits
11111111111111111111111111111111111111111111111111111111111111
After two complementary front
1111111111111111111111111111111111111111111111111111111111111111
after the decimal result becomes -1
so
-100 >>> 2 = -1
* /
</ Script>

Guess you like

Origin www.cnblogs.com/huchong-bk/p/11204858.html