예제 #1
0
        public static long ToLong(string s, TextFormatIntegerStyle style)
        {
            if (s.Length == 0)
            {
                return(0L);
            }
            switch (style)
            {
            case TextFormatIntegerStyle.Binary:
                return((int)Convert.ToInt64(s, 2));

            case TextFormatIntegerStyle.Octal:
                return((int)Convert.ToInt64(s, 8));

            case TextFormatIntegerStyle.Integer:
                return((int)Convert.ToInt64(s, 10));

            case TextFormatIntegerStyle.Hexadecimal:
                return((int)Convert.ToInt64(s, 16));

            default:
                return(0L);
            }
        }
예제 #2
0
        public static string ToString(long value, TextFormatIntegerStyle style, int fixedLength, LeadingStyle leadingStyle)
        {
            long   value2 = (style != 0) ? value : Math.Abs(value);
            string text;

            switch (style)
            {
            case TextFormatIntegerStyle.Binary:
                text = Convert2.ToString(value2, 2);
                goto IL_0048;

            case TextFormatIntegerStyle.Octal:
                text = Convert2.ToString(value2, 8);
                goto IL_0048;

            case TextFormatIntegerStyle.Integer:
                text = Convert2.ToString(value2, 10);
                goto IL_0048;

            case TextFormatIntegerStyle.Hexadecimal:
                text = Convert2.ToString(value2, 16);
                goto IL_0048;

            default:
            {
                return("Not-Supported");
            }
IL_0048:
                text = text.ToUpper(CultureInfo.CurrentCulture);
                if (fixedLength != 0)
                {
                    string value3;
                    if (value < 0L)
                    {
                        switch (style)
                        {
                        case TextFormatIntegerStyle.Binary:
                            value3 = "1";
                            break;

                        case TextFormatIntegerStyle.Octal:
                            value3 = "7";
                            break;

                        case TextFormatIntegerStyle.Integer:
                            value3 = "0";
                            break;

                        case TextFormatIntegerStyle.Hexadecimal:
                            value3 = "F";
                            break;

                        default:
                            value3 = "0";
                            break;
                        }
                    }
                    else
                    {
                        value3 = ((leadingStyle != LeadingStyle.Zeros) ? " " : "0");
                    }
                    if (text.Length > fixedLength)
                    {
                        text = text.Substring(text.Length - fixedLength, fixedLength);
                    }
                    StringBuilder stringBuilder = new StringBuilder(text);
                    for (int i = 0; i < fixedLength - text.Length; i++)
                    {
                        stringBuilder.Insert(0, value3);
                    }
                    text = stringBuilder.ToString();
                }
                if (style == TextFormatIntegerStyle.Integer && value < 0L)
                {
                    text = '-' + text;
                }
                return(text);
            }
        }