5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
10 using System.Globalization;
14 public struct Byte : IComparable, IFormattable { //, IConvertible {
15 private static Type Type = typeof (byte);
17 public const byte MinValue = 0;
18 public const byte MaxValue = 255;
20 // VES needs to know about value. public is workaround
21 // so source will compile
24 public int CompareTo (object v)
29 if (!(v is System.Byte))
30 throw new ArgumentException (Locale.GetText ("Value is not a System.Byte"));
42 public override bool Equals (object o)
44 if (!(o is System.Byte))
47 return ((byte) o) == value;
50 public override int GetHashCode ()
55 public static byte Parse (string s)
60 bool digits_seen = false;
63 throw new ArgumentNullException (Locale.GetText ("s is null"));
67 // look for the first non-whitespace character
69 for (i = 0; i < len; i++){
71 if (!Char.IsWhiteSpace (c))
75 // if it's all whitespace, then throw exception
77 throw new FormatException ();
79 // look for the optional '+' sign
83 // we should just have numerals followed by whitespace now
87 if (c >= '0' && c <= '9'){
88 // shift left and accumulate every time we find a numeral
89 byte d = (byte) (c - '0');
91 val = checked ((byte) (val * 10 + d));
94 // after the last numeral, only whitespace is allowed
95 if (Char.IsWhiteSpace (c)){
96 for (i++; i < len; i++){
97 if (!Char.IsWhiteSpace (s [i]))
98 throw new FormatException ();
102 throw new FormatException ();
106 // if all we had was a '+' sign, then throw exception
108 throw new FormatException ();
113 public static byte Parse (string s, IFormatProvider fp)
115 return Parse (s, NumberStyles.Integer, fp);
118 public static byte Parse (string s, NumberStyles style)
120 return Parse (s, style, null);
124 public static byte Parse (string s, NumberStyles style, IFormatProvider fp)
127 throw new ArgumentNullException();
130 // TODO: Handle other styles and FormatProvider properties
131 throw new NotImplementedException();
134 public override string ToString ()
136 return ToString ("G", null);
139 public string ToString (IFormatProvider fp)
141 return ToString ("G", fp);
144 public string ToString (string format)
146 return ToString (format, null);
149 public string ToString (string format, IFormatProvider fp)
152 NumberFormatInfo nfi;
154 fmt = (format == null) ? "G" : format;
157 nfi = NumberFormatInfo.CurrentInfo;
159 nfi = (NumberFormatInfo) fp.GetFormat (Type);
162 nfi = NumberFormatInfo.CurrentInfo;
165 return IntegerFormatter.NumberToString (fmt, nfi, value);
168 // =========== IConvertible Methods =========== //
170 public TypeCode GetTypeCode ()
172 return TypeCode.Byte;