请将以下代码转换成C#代码,谢谢!function _hex(i){
return("0123456789ABCDEF".substring(i,i+1))
}function to_hex(i){ var c1=_hex((0x0000f0&i)>>4);
var c2=_hex((0x00000f&i)>>0);
if(c1!="0"){
return(c1+c2)
}else{
return c2
}
}function to_unsigned(i){
if(i<0){
return(i+4294967296);
}else if(i>=4294967296){
return(i-4294967296);
}else{
return i;
}
}
function str_hash(str){
var ch;
var hash=5381;
var len=str.length;
for(var i=0;i<len;i++){

ch=str.charCodeAt(i)-65;

if(ch<0){ch+=256}
if(ch<=25){ch+=32}

var tmp=hash<<5;

tmp=to_unsigned(tmp);
hash+=tmp;
hash=to_unsigned(hash);
hash=(hash^ch);
hash=to_unsigned(hash)
}

return hash;
}function hash_path(docid){ var hash = str_hash(docid);
return(to_hex(hash%128)+"/"+to_hex(hash%255))
}

解决方案 »

  1.   

    不知道对不对:)
       public string _hex(long i)
            {
                return "0123456789ABCDEF".Substring((int)i, (int)i + 1);
            }
            public string to_hex(long i)
            {            string c1 = _hex((0x0000f0 & i) >> 4);
                string c2 = _hex((0x00000f & i) >> 0);
                if (c1 != "0")
                {
                    return (c1 + c2);
                }
                else
                {
                    return c2;
                }
            }
            public long to_unsigned(long i)
            {
                if (i < 0)
                {
                    return (i + 4294967296);
                }
                else if (i >= 4294967296)
                {
                    return (i - 4294967296);
                }
                else
                {
                    return i;
                }
            }
            public long str_hash(string str)
            {
                int ch;
                long hash = 5381;
                int len = str.Length;
                for (int i = 0; i < len; i++)
                {
                    ch = str[i] - 65;                if (ch < 0) ch += 256;
                    if (ch <= 25) ch += 32;                long tmp = hash << 5;                tmp = to_unsigned(tmp);
                    hash += tmp;
                    hash = to_unsigned(hash);
                    hash = (hash ^ ch);
                    hash = to_unsigned(hash);
                }            return hash;
            }
            public string hash_path(string docid)
            {            long hash = str_hash(docid);
                return (to_hex(hash % 128) + "/" + to_hex(hash % 255));
            }
      

  2.   


    function _hex(i){
        return("0123456789ABCDEF".substring(i,i+1))
    }
    pubic String _hex(int i)
    {
      return("0123456789ABCDEF".Substring(i,i+1))
    }function to_hex(i){    var c1=_hex((0x0000f0&i)>>4);
        var c2=_hex((0x00000f&i)>>0);
        if(c1!="0"){
            return(c1+c2)
        }else{
            return c2
        }
    }
    public string to_hex(i)
    {
      String c1=_hex((0x0000fx&i)>>4);
      String c2=_hex((0x00000f&i)>>0);
     if(c1!="0")
     {
     return(c1+c2)
     }
     else{
            return c2
         }
    }function to_unsigned(i){
        if(i<0){
            return(i+4294967296);
        }else if(i>=4294967296){
            return(i-4294967296);
        }else{
            return i;
        }
    }
     public string  to_unsigned(int i)
     {
       if(i<0)
       {
        return(i+4294967296);
       }
       else if(i>=4294967296){
            return(i-4294967296);
        }else{
            return i;
        }
      }   function str_hash(str){
        var ch;
        var hash=5381;
        var len=str.length;
        for(var i=0;i<len;i++){
        
            ch=str.charCodeAt(i)-65;
            
            if(ch<0){ch+=256}
            if(ch<=25){ch+=32}
            
            var tmp=hash<<5;
            
            tmp=to_unsigned(tmp);
            hash+=tmp;
            hash=to_unsigned(hash);
            hash=(hash^ch);
            hash=to_unsigned(hash)
        }
        
        return hash;
    }
    public string str_hash(string str){
        int ch;
        int hash=5381;
        int len=str.length;
        for(int i=0;i<len;i++)
         {
        
            ch=str.charCodeAt(i)-65;
            
            if(ch<0){ch+=256}
            if(ch<=25){ch+=32}
            
            int tmp=hash<<5;
            
            tmp=to_unsigned(tmp);
            hash+=tmp;
            hash=to_unsigned(hash);
            hash=(hash^ch);
            hash=to_unsigned(hash)
        }
        
        return hash;
    }
    function hash_path(docid){    var hash = str_hash(docid);
        return(to_hex(hash%128)+"/"+to_hex(hash%255))
    }
    public String hash_path(String docid){    String hash = str_hash(docid);
        return(to_hex(hash%128)+"/"+to_hex(hash%255))
    }
    基本应该就是这样的!你可以试试!
      

  3.   

    to_unsigned  类型应该是ulong不过
    if(i<0){
            return(i+4294967296);
    4294967296 正好是uint 上限加1
    所以类型应该是uint
      

  4.   

        public string _hex(uint i)
        {
            return "0123456789ABCDEF".Substring((int)i, (int)i + 1);
        }
        public string to_hex(uint i)
        {        string c1 = _hex((uint)((0x0000f0 & i) >> 4));
            string c2 = _hex((uint)((0x00000f & i) >> 0));
            if (c1 != "0")
            {
                return (c1 + c2);
            }
            else
            {
                return c2;
            }
        }
        public uint to_unsigned(uint i)
        {
            if (i < 0)
            {
                return (i + 4294967295);
            }
            else if (i >= 4294967295)
            {
                return (i - 4294967295);
            }
            else
            {
                return i;
            }
        }
        public uint str_hash(string str)
        {
            int ch;
            uint hash = 5381;
            int len = str.Length;
            for (int i = 0; i < len; i++)
            {
                ch = str[i] - 65;            if (ch < 0) ch += 256;
                if (ch <= 25) ch += 32;            uint tmp = hash << 5;            tmp = to_unsigned(tmp);
                hash += tmp;
                hash = to_unsigned(hash);
                hash = (uint)(hash ^ ch);
                hash = to_unsigned(hash);
            }        return hash;
        }
        public string hash_path(string docid)
        {        uint hash = str_hash(docid);
            return (to_hex(hash % 128) + "/" + to_hex(hash % 255));
        }
      

  5.   

    谢谢 ,Sandy945 ,用你的程序搞定,不过有个小地方改下:    public string _hex(uint i)
        {
            return "0123456789ABCDEF".Substring((int)i, 1);
        }