现在的位置: 首页 > 综合 > 正文

字符串转换成十六进制(包含汉字)c#

2013年09月08日 ⁄ 综合 ⁄ 共 890字 ⁄ 字号 评论关闭

public static string Encodeing(string input)
    {
        char[] values = input.ToCharArray();
        byte[] bvalues = Encoding.Default.GetBytes(values);

        string sResult = "";
        int nLoop = 0;
        string hex = "0123456789ABCDEF";
        while (nLoop < bvalues.Length)
        {
            byte abyte = bvalues[nLoop];
            if (abyte < 127 && abyte > 0)
            {//
                sResult += '0';
                sResult += '0';
                sResult += hex[(abyte >> 4) & 0x0f];
                sResult += hex[abyte & 0x0f];
                nLoop++;
            }
            else
            {//
                sResult += hex[(abyte >> 4) & 0x0f];
                sResult += hex[abyte & 0x0f];
                nLoop++;

                if (nLoop == bvalues.Length) break;

                abyte = bvalues[nLoop];
                sResult += hex[(abyte >> 4) & 0x0f];
                sResult += hex[abyte & 0x0f];
                nLoop++;
            }
        }
        return sResult;
    }

抱歉!评论已关闭.