首页 > 学院 > 开发设计 > 正文

16进制字符串转整数(16进制颜色值 如#f0f011)

2019-11-08 01:17:51
字体:
来源:转载
供稿:网友
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace BaseServer{    public static class StringHexToInt    {        public static int StrHexToInt(string str)        {            char[] chars = str.ToCharArray();            int len = chars.Length;            int i=0;            int result=0;            for (i = 0; i < len;i++ )            {                if (!(chars[i] == 'a' || chars[i] == 'A'||                    chars[i] == 'b' || chars[i] == 'B' ||                    chars[i] == 'c' || chars[i] == 'C' ||                    chars[i] == 'd' || chars[i] == 'D' ||                    chars[i] == 'e' || chars[i] == 'E' ||                    chars[i] == 'f' || chars[i] == 'F')                     )                {                   int P16 = (int) Math.Pow(16,len-1-i );                   result += int.Parse(chars[i].ToString()) * P16;                }                else                {                    if(chars[i] == 'a' || chars[i] == 'A')                    {                        int P16 = (int)Math.Pow(16, len - 1 - i);                        result += 10 * P16;                    }                    else if (chars[i] == 'b' || chars[i] == 'B')                    {                        int P16 = (int)Math.Pow(16, len - 1 - i);                        result += 11 * P16;                    }                    else if (chars[i] == 'c' || chars[i] == 'C')                    {                        int P16 = (int)Math.Pow(16, len - 1 - i);                        result += 12 * P16;                    }                    else if (chars[i] == 'd' || chars[i] == 'D')                    {                        int P16 = (int)Math.Pow(16, len - 1 - i);                        result += 13* P16;                    }                    else if (chars[i] == 'e' || chars[i] == 'E')                    {                        int P16 = (int)Math.Pow(16, len - 1 - i);                        result += 14 * P16;                    }                    else if (chars[i] == 'f' || chars[i] == 'F')                    {                        int P16 = (int)Math.Pow(16, len - 1 - i);                        result += 15 * P16;                    }                }            }            return result;        }       }}
上一篇:理解指针

下一篇:34.缓存

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表