-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[js] ip地址解析 #12
Comments
|
//本题其实就是考二进制,十进制之间的转换,贴一个没有容错处理的吧
function decodeIp(int32) {
var byt = int32.toString(2), //2进制表示
reg = /(?:[0-1]{8})/g; // 每八位一组
//TODO 后期看看能有什么优化
return byt.match(reg).map(function(num){
return parseInt(num,2);//转换为十进制
}).join(".");
} |
@langmao 写的不错。我也贴个有趣的,位计算 function decodeIp(int32) {
return [ int32 / 2 >> 23, (int32 / 2 >> 15) % 256, (int32 / 2 >> 7) % 256, int32 % 256 ].join(".");
} |
@VaJoy 6666666666 v神 |
@VaJoy 绝对牛逼!!! |
正好以前写过,凑个热闹 |
果断收藏了 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: