标签:style blog http io color ar sp java for
新浪微博每一条微博都会有一个mid,然后每条微博都有一个独立的地址,例如:http://www.weibo.com/2722086301/Bw3SXzWzP
规律:地址中的黄色部分是用户id,绿色部分是微博的识别字符串,经过分析应该是62进制的表示。每调微博有一个编号称为mid,是十进制的。所以mid和地址中的绿色部分可以相互转换,转换的js代码如下:
一下代码来自互联网,版权归原作者所有。我只是借用一下
/** * 新浪微博mid与url互转实用工具 * 作者: XiNGRZ (http://weibo.com/xingrz) */ var WeiboUtility = {}; /** * 62进制字典 */ WeiboUtility.str62keys = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" ]; /** * 62进制值转换为10进制 * @param {String} str62 62进制值 * @return {String} 10进制值 */ WeiboUtility.str62to10 = function(str62) { var i10 = 0; for (var i = 0; i < str62.length; i++) { var n = str62.length - i - 1; var s = str62[i]; i10 += this.str62keys.indexOf(s) * Math.pow(62, n); } return i10; }; /** * 10进制值转换为62进制 * @param {String} int10 10进制值 * @return {String} 62进制值 */ WeiboUtility.int10to62 = function(int10) { var s62 = ‘‘; var r = 0; while (int10 != 0) { r = int10 % 62; s62 = this.str62keys[r] + s62; int10 = Math.floor(int10 / 62); } return s62; }; /** * URL字符转换为mid * @param {String} url 微博URL字符,如 "wr4mOFqpbO" * @return {String} 微博mid,如 "201110410216293360" */ WeiboUtility.url2mid = function(url) { var mid = ‘‘; for (var i = url.length - 4; i > -4; i = i - 4) //从最后往前以4字节为一组读取URL字符 { var offset1 = i < 0 ? 0 : i; var offset2 = i + 4; var str = url.substring(offset1, offset2); str = this.str62to10(str); if (offset1 > 0) //若不是第一组,则不足7位补0 { while (str.length < 7) { str = ‘0‘ + str; } } mid = str + mid; } return mid; }; /** * mid转换为URL字符 * @param {String} mid 微博mid,如 "201110410216293360" * @return {String} 微博URL字符,如 "wr4mOFqpbO" */ WeiboUtility.mid2url = function(mid) { if (typeof(mid) != ‘string‘) return false; //mid数值较大,必须为字符串! var url = ‘‘; for (var i = mid.length - 7; i > -7; i = i - 7) //从最后往前以7字节为一组读取mid { var offset1 = i < 0 ? 0 : i; var offset2 = i + 7; var num = mid.substring(offset1, offset2); num = this.int10to62(num); url = num + url; } return url; }; document.write(WeiboUtility.mid2url("3775437480334921"));
标签:style blog http io color ar sp java for
原文地址:http://www.cnblogs.com/dragondean/p/js-weibo-mid-url.html