新浪微博mid与id互转实用工具(转)

js版 

/**
* 新浪微博mid和id转换工具类
* @constructor 工具类  提供10进制和62进制转换
*/
function SinaWeiboUtility() {
    /**
    * 62进制字典
    * @property {String}
    
*/
    this.str62keys = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    if (typeof (SinaWeiboUtility._initalized) == 'undefined') {
        /**
        * 10进制值转换为62进制
        * @param {String} int10 10进制值
        * @return {String} 62进制值
        
*/
        SinaWeiboUtility.prototype.int10to62 = function (int10) {
            var s62 = '';
            var r = 0;
            while (int10 != 0) {
                r = int10 % 62;
                s62 = this.str62keys.charAt(r) + s62;
                int10 = Math.floor(int10 / 62);
            }
            return s62;
        };
        /**
        * 62进制值转换为10进制
        * @param {String} str62 62进制值
        * @return {String} 10进制值
        
*/
        SinaWeiboUtility.prototype.str62to10 = function (str62) {
            var i10 = 0;
            for (var i = 0; i < str62.length; i++) {
                var n = str62.length - i - 1;
                var s = str62.substr(i, 1);  // str62[i]; 字符串用数组方式获取,IE下不支持为“undefined”
                i10 += parseInt(this.str62keys.indexOf(s)) * Math.pow(62, n);
            }
            return i10;
        };
        /**
        * id转换为mid
        * @param {String} id 微博id,如 "201110410216293360"
        * @return {String} 微博mid,如 "wr4mOFqpbO"
        
*/
        SinaWeiboUtility.prototype.id2mid = function (id) {
            if (typeof (id) != 'string') {
                return false// id数值较大,必须为字符串!
            }
            var mid = '';

            for (var i = id.length - 7; i > -7; i = i - 7//从最后往前以7字节为一组读取mid
            {
                var offset1 = i < 0 ? 0 : i;
                var offset2 = i + 7;
                var num = id.substring(offset1, offset2);

                num = this.int10to62(num);
                mid = num + mid;
            }

            return mid;
        };
        /**
        * mid转换为id
        * @param {String} mid 微博mid,如 "wr4mOFqpbO"
        * @return {String} 微博id,如 "201110410216293360"
        
*/
        SinaWeiboUtility.prototype.mid2id = function (mid) {
            var id = '';

            for (var i = mid.length - 4; i > -4; i = i - 4//从最后往前以4字节为一组读取mid字符
            {
                var offset1 = i < 0 ? 0 : i;
                var len = i < 0 ? parseInt(mid.length % 4) : 4;
                var str = mid.substr(offset1, len);

                str = this.str62to10(str).toString();
                if (offset1 > 0//若不是第一组,则不足7位补0
                {
                    while (str.length < 7) {
                        str = '0' + str;
                    }
                }

                id = str + id;
            }
            return id;
        };

        /**
        * 标识对象是否初始化过(将方法挂在prototype上只需执行一次即可,不必要的重复的运算。)
        * @property {boolean}
        
*/
        SinaWeiboUtility._initalized = true;
    }
}

 

c#版

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace SinaExportTool.Public
{
    /// <summary>
    
/// 新浪微博mid和id转换工具类
    
/// </summary>
    public class SinaWeiboUtility
    {

        /// <summary> 
        
/// 从一条新浪微博地址中获取这条微博的id
        
/// </summary>  
        
/// <param name="url">一条微博地址</param>  
        
/// <returns>微博id</returns>  
        public static string GetIdFromUrl(string url)
        {
            string mid = GetMidFromUrl(url);

            if (string.IsNullOrEmpty(mid))
            {
                return string.Empty;

            }
            else
            {
                return Mid2Id(mid);

            }
        }

        /// <summary> 
        
/// 从一条新浪微博地址中获取这条微博的mid
        
/// </summary>  
        
/// <param name="url">一条微博地址</param>  
        
/// <returns>微博mid</returns>  
        public static string GetMidFromUrl(string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                return "";
            }
            if (url.IndexOf('?') != -1)
            {
                url = url.Substring(0, url.IndexOf('?'));
            }
            Regex reg = new Regex(@"^http://(e\.)?weibo\.com/[0-9a-zA-Z]+/(?<id>[0-9a-zA-Z]+)$", RegexOptions.IgnoreCase);
            Match match = reg.Match(url);
            if (match.Success)
            {
                return match.Result("${id}");
            }
            return "";
        }

        /// <summary>
        
/// 将新浪微博mid转换成id
        
/// </summary>
        
/// <param name="mid">微博mid</param>
        
/// <returns>返回微博的id</returns>
        public static string Mid2Id(string mid)
        {
            string id = "";

            for (int i = mid.Length - 4; i > -4; i = i - 4//从最后往前以4字节为一组读取URL字符
            {
                int offset1 = i < 0 ? 0 : i;
                int len = i < 0 ? mid.Length % 4 : 4;
                var str = mid.Substring(offset1, len);

                str = Str62toInt(str);
                if (offset1 > 0//若不是第一组,则不足7位补0
                {
                    while (str.Length < 7)
                    {
                        str = "0" + str;
                    }
                }
                id = str + id;
            }

            return id;
        }

        /// <summary>
        
/// 新浪微博id转换为mid
        
/// </summary>
        
/// <param name="id">微博id</param>
        
/// <returns>返回微博的mid</returns>
        public static string Id2Mid(string id)
        {
            string mid = "", strTemp;
            int startIdex, len;

            for (var i = id.Length - 7; i > -7; i = i - 7//从最后往前以7字节为一组读取mid
            {
                startIdex = i < 0 ? 0 : i;

                len = i < 0 ? id.Length % 7 : 7;

                strTemp = id.Substring(startIdex, len);

                mid = IntToStr62(Convert.ToInt32(strTemp)) + mid;
            }
            return mid;
        }

        //62进制转成10进制
        public static string Str62toInt(string str62)
        {
            Int64 i64 = 0;

            for (int i = 0; i < str62.Length; i++)
            {
                Int64 Vi = (Int64)Math.Pow(62, (str62.Length - i - 1));

                char t = str62[i];

                i64 += Vi * GetInt10(t.ToString());

            }
            return i64.ToString();
        }

        //10进制转成62进制
        public static string IntToStr62(int int10)
        {
            string s62 = "";
            int r = 0;
            while (int10 != 0)
            {
                r = int10 % 62;
                s62 = Get62key(r) + s62;
                int10 = int10 / 62;
            }
            return s62;
        }

 

         // 62进制字典
        private static string str62keys = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVTXYZ";

        //获取key对应的62进制整数  
        private static Int64 GetInt10(string key)
        {
            return str62keys.IndexOf(key);
        }

        //获取62进制整数对应的key
        private static string Get62key(int int10)
        {
            if (int10 < 0 || int10 > 61)
                return "";
            return str62keys.Substring(int10, 1);
        }
    }

}

 

posted @ 2011-11-22 10:23  贝加  阅读(4691)  评论(0编辑  收藏  举报