| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.Buffers.Text; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Diagnostics.CodeAnalysis; |
| | | 8 | | using System.Globalization; |
| | | 9 | | using System.Runtime.CompilerServices; |
| | | 10 | | using System.Runtime.InteropServices; |
| | | 11 | | using System.Runtime.Intrinsics; |
| | | 12 | | using System.Text; |
| | | 13 | | |
| | | 14 | | namespace System |
| | | 15 | | { |
| | | 16 | | [Flags] |
| | | 17 | | public enum Base64FormattingOptions |
| | | 18 | | { |
| | | 19 | | None = 0, |
| | | 20 | | InsertLineBreaks = 1 |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | // The Convert class provides conversion and querying methods for values. The |
| | | 24 | | // Convert class contains static members only, and it is not possible to create |
| | | 25 | | // instances of the class. |
| | | 26 | | // |
| | | 27 | | // The statically typed conversion methods provided by the Convert class are all |
| | | 28 | | // of the form: |
| | | 29 | | // |
| | | 30 | | // public static XXX ToXXX(YYY value) |
| | | 31 | | // |
| | | 32 | | // where XXX is the target type and YYY is the source type. The matrix below |
| | | 33 | | // shows the set of supported conversions. The set of conversions is symmetric |
| | | 34 | | // such that for every ToXXX(YYY) there is also a ToYYY(XXX). |
| | | 35 | | // |
| | | 36 | | // From: To: Bol Chr SBy Byt I16 U16 I32 U32 I64 U64 Sgl Dbl Dec Dat Str |
| | | 37 | | // ---------------------------------------------------------------------- |
| | | 38 | | // Boolean x x x x x x x x x x x x x |
| | | 39 | | // Char x x x x x x x x x x |
| | | 40 | | // SByte x x x x x x x x x x x x x x |
| | | 41 | | // Byte x x x x x x x x x x x x x x |
| | | 42 | | // Int16 x x x x x x x x x x x x x x |
| | | 43 | | // UInt16 x x x x x x x x x x x x x x |
| | | 44 | | // Int32 x x x x x x x x x x x x x x |
| | | 45 | | // UInt32 x x x x x x x x x x x x x x |
| | | 46 | | // Int64 x x x x x x x x x x x x x x |
| | | 47 | | // UInt64 x x x x x x x x x x x x x x |
| | | 48 | | // Single x x x x x x x x x x x x x |
| | | 49 | | // Double x x x x x x x x x x x x x |
| | | 50 | | // Decimal x x x x x x x x x x x x x |
| | | 51 | | // DateTime x x |
| | | 52 | | // String x x x x x x x x x x x x x x x |
| | | 53 | | // ---------------------------------------------------------------------- |
| | | 54 | | // |
| | | 55 | | // For dynamic conversions, the Convert class provides a set of methods of the |
| | | 56 | | // form: |
| | | 57 | | // |
| | | 58 | | // public static XXX ToXXX(object value) |
| | | 59 | | // |
| | | 60 | | // where XXX is the target type (Boolean, Char, SByte, Byte, Int16, UInt16, |
| | | 61 | | // Int32, UInt32, Int64, UInt64, Single, Double, Decimal, DateTime, |
| | | 62 | | // or String). The implementations of these methods all take the form: |
| | | 63 | | // |
| | | 64 | | // public static XXX toXXX(object value) { |
| | | 65 | | // return value == null? XXX.Default: ((IConvertible)value).ToXXX(); |
| | | 66 | | // } |
| | | 67 | | // |
| | | 68 | | // The code first checks if the given value is a null reference (which is the |
| | | 69 | | // same as TypeCode.Empty), in which case it returns the default value for type |
| | | 70 | | // XXX. Otherwise, a cast to IConvertible is performed, and the appropriate ToXXX() |
| | | 71 | | // method is invoked on the object. An InvalidCastException is thrown if the |
| | | 72 | | // cast to IConvertible fails, and that exception is simply allowed to propagate out |
| | | 73 | | // of the conversion method. |
| | | 74 | | |
| | | 75 | | public static partial class Convert |
| | | 76 | | { |
| | | 77 | | private const int Base64LineBreakPosition = 76; |
| | | 78 | | |
| | | 79 | | // Constant representing the database null value. This value is used in |
| | | 80 | | // database applications to indicate the absence of a known value. Note |
| | | 81 | | // that Convert.DBNull is NOT the same as a null object reference, which is |
| | | 82 | | // represented by TypeCode.Empty. |
| | | 83 | | // |
| | | 84 | | // When passed Convert.DBNull, the Convert.GetTypeCode() method returns |
| | | 85 | | // TypeCode.DBNull. |
| | | 86 | | // |
| | | 87 | | // When passed Convert.DBNull, all the Convert.ToXXX() methods except ToString() |
| | | 88 | | // throw an InvalidCastException. |
| | 0 | 89 | | public static readonly object DBNull = System.DBNull.Value; |
| | | 90 | | |
| | | 91 | | // Returns the type code for the given object. If the argument is null, |
| | | 92 | | // the result is TypeCode.Empty. If the argument is not a value (i.e. if |
| | | 93 | | // the object does not implement IConvertible), the result is TypeCode.Object. |
| | | 94 | | // Otherwise, the result is the type code of the object, as determined by |
| | | 95 | | // the object's implementation of IConvertible. |
| | | 96 | | public static TypeCode GetTypeCode(object? value) |
| | | 97 | | { |
| | 0 | 98 | | if (value == null) return TypeCode.Empty; |
| | 0 | 99 | | if (value is IConvertible temp) |
| | | 100 | | { |
| | 0 | 101 | | return temp.GetTypeCode(); |
| | | 102 | | } |
| | 0 | 103 | | return TypeCode.Object; |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | // Returns true if the given object is a database null. This operation |
| | | 107 | | // corresponds to "value.GetTypeCode() == TypeCode.DBNull". |
| | | 108 | | public static bool IsDBNull([NotNullWhen(true)] object? value) |
| | | 109 | | { |
| | 0 | 110 | | if (value == System.DBNull.Value) return true; |
| | 0 | 111 | | return value is IConvertible convertible ? convertible.GetTypeCode() == TypeCode.DBNull : false; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | // Converts the given object to the given type. In general, this method is |
| | | 115 | | // equivalent to calling ((IConvertible)value).ToXXX(CultureInfo.CurrentCulture) for the given |
| | | 116 | | // typeCode and boxing the result. |
| | | 117 | | // |
| | | 118 | | // The method first checks if the given object implements IConvertible. If not, |
| | | 119 | | // the only permitted conversion is from a null to TypeCode.Empty/TypeCode.String/TypeCode.Object, the |
| | | 120 | | // result of which is null. |
| | | 121 | | [return: NotNullIfNotNull(nameof(value))] |
| | | 122 | | public static object? ChangeType(object? value, TypeCode typeCode) |
| | | 123 | | { |
| | 0 | 124 | | return ChangeType(value, typeCode, CultureInfo.CurrentCulture); |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | [return: NotNullIfNotNull(nameof(value))] |
| | | 128 | | public static object? ChangeType(object? value, TypeCode typeCode, IFormatProvider? provider) |
| | | 129 | | { |
| | 0 | 130 | | if (value == null && (typeCode == TypeCode.Empty || typeCode == TypeCode.String || typeCode == TypeCode.Obje |
| | | 131 | | { |
| | 0 | 132 | | return null; |
| | | 133 | | } |
| | | 134 | | |
| | 0 | 135 | | if (value is not IConvertible v) |
| | | 136 | | { |
| | 0 | 137 | | throw new InvalidCastException(SR.InvalidCast_IConvertible); |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | // This line is invalid for things like Enums that return a TypeCode |
| | | 141 | | // of int, but the object can't actually be cast to an int. |
| | | 142 | | // if (v.GetTypeCode() == typeCode) return value; |
| | 0 | 143 | | return typeCode switch |
| | 0 | 144 | | { |
| | 0 | 145 | | TypeCode.Boolean => v.ToBoolean(provider), |
| | 0 | 146 | | TypeCode.Char => v.ToChar(provider), |
| | 0 | 147 | | TypeCode.SByte => v.ToSByte(provider), |
| | 0 | 148 | | TypeCode.Byte => v.ToByte(provider), |
| | 0 | 149 | | TypeCode.Int16 => v.ToInt16(provider), |
| | 0 | 150 | | TypeCode.UInt16 => v.ToUInt16(provider), |
| | 0 | 151 | | TypeCode.Int32 => v.ToInt32(provider), |
| | 0 | 152 | | TypeCode.UInt32 => v.ToUInt32(provider), |
| | 0 | 153 | | TypeCode.Int64 => v.ToInt64(provider), |
| | 0 | 154 | | TypeCode.UInt64 => v.ToUInt64(provider), |
| | 0 | 155 | | TypeCode.Single => v.ToSingle(provider), |
| | 0 | 156 | | TypeCode.Double => v.ToDouble(provider), |
| | 0 | 157 | | TypeCode.Decimal => v.ToDecimal(provider), |
| | 0 | 158 | | TypeCode.DateTime => v.ToDateTime(provider), |
| | 0 | 159 | | TypeCode.String => v.ToString(provider), |
| | 0 | 160 | | TypeCode.Object => value, |
| | 0 | 161 | | TypeCode.DBNull => throw new InvalidCastException(SR.InvalidCast_DBNull), |
| | 0 | 162 | | TypeCode.Empty => throw new InvalidCastException(SR.InvalidCast_Empty), |
| | 0 | 163 | | _ => throw new ArgumentException(SR.Arg_UnknownTypeCode), |
| | 0 | 164 | | }; |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | internal static object DefaultToType(IConvertible value, Type targetType, IFormatProvider? provider) |
| | | 168 | | { |
| | 0 | 169 | | ArgumentNullException.ThrowIfNull(targetType); |
| | | 170 | | |
| | 0 | 171 | | Debug.Assert(value != null, "[Convert.DefaultToType]value!=null"); |
| | | 172 | | |
| | 0 | 173 | | if (ReferenceEquals(value.GetType(), targetType)) |
| | | 174 | | { |
| | 0 | 175 | | return value; |
| | | 176 | | } |
| | | 177 | | |
| | 0 | 178 | | if (ReferenceEquals(targetType, typeof(bool))) |
| | 0 | 179 | | return value.ToBoolean(provider); |
| | 0 | 180 | | if (ReferenceEquals(targetType, typeof(char))) |
| | 0 | 181 | | return value.ToChar(provider); |
| | 0 | 182 | | if (ReferenceEquals(targetType, typeof(sbyte))) |
| | 0 | 183 | | return value.ToSByte(provider); |
| | 0 | 184 | | if (ReferenceEquals(targetType, typeof(byte))) |
| | 0 | 185 | | return value.ToByte(provider); |
| | 0 | 186 | | if (ReferenceEquals(targetType, typeof(short))) |
| | 0 | 187 | | return value.ToInt16(provider); |
| | 0 | 188 | | if (ReferenceEquals(targetType, typeof(ushort))) |
| | 0 | 189 | | return value.ToUInt16(provider); |
| | 0 | 190 | | if (ReferenceEquals(targetType, typeof(int))) |
| | 0 | 191 | | return value.ToInt32(provider); |
| | 0 | 192 | | if (ReferenceEquals(targetType, typeof(uint))) |
| | 0 | 193 | | return value.ToUInt32(provider); |
| | 0 | 194 | | if (ReferenceEquals(targetType, typeof(long))) |
| | 0 | 195 | | return value.ToInt64(provider); |
| | 0 | 196 | | if (ReferenceEquals(targetType, typeof(ulong))) |
| | 0 | 197 | | return value.ToUInt64(provider); |
| | 0 | 198 | | if (ReferenceEquals(targetType, typeof(float))) |
| | 0 | 199 | | return value.ToSingle(provider); |
| | 0 | 200 | | if (ReferenceEquals(targetType, typeof(double))) |
| | 0 | 201 | | return value.ToDouble(provider); |
| | 0 | 202 | | if (ReferenceEquals(targetType, typeof(decimal))) |
| | 0 | 203 | | return value.ToDecimal(provider); |
| | 0 | 204 | | if (ReferenceEquals(targetType, typeof(DateTime))) |
| | 0 | 205 | | return value.ToDateTime(provider); |
| | 0 | 206 | | if (ReferenceEquals(targetType, typeof(string))) |
| | 0 | 207 | | return value.ToString(provider); |
| | 0 | 208 | | if (ReferenceEquals(targetType, typeof(object))) |
| | 0 | 209 | | return (object)value; |
| | | 210 | | // Need to special case Enum because typecode will be underlying type, e.g. Int32 |
| | 0 | 211 | | if (ReferenceEquals(targetType, typeof(Enum))) |
| | 0 | 212 | | return (Enum)value; |
| | 0 | 213 | | if (ReferenceEquals(targetType, typeof(DBNull))) |
| | 0 | 214 | | throw new InvalidCastException(SR.InvalidCast_DBNull); |
| | 0 | 215 | | if (ReferenceEquals(targetType, typeof(Empty))) |
| | 0 | 216 | | throw new InvalidCastException(SR.InvalidCast_Empty); |
| | | 217 | | |
| | 0 | 218 | | throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, value.GetType().FullName, targetType.FullNam |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | [return: NotNullIfNotNull(nameof(value))] |
| | | 222 | | public static object? ChangeType(object? value, Type conversionType) |
| | | 223 | | { |
| | 0 | 224 | | return ChangeType(value, conversionType, CultureInfo.CurrentCulture); |
| | | 225 | | } |
| | | 226 | | |
| | | 227 | | [return: NotNullIfNotNull(nameof(value))] |
| | | 228 | | public static object? ChangeType(object? value, Type conversionType, IFormatProvider? provider) |
| | | 229 | | { |
| | 0 | 230 | | ArgumentNullException.ThrowIfNull(conversionType); |
| | | 231 | | |
| | 0 | 232 | | if (value == null) |
| | | 233 | | { |
| | 0 | 234 | | if (conversionType.IsValueType) |
| | | 235 | | { |
| | 0 | 236 | | throw new InvalidCastException(SR.InvalidCast_CannotCastNullToValueType); |
| | | 237 | | } |
| | 0 | 238 | | return null; |
| | | 239 | | } |
| | | 240 | | |
| | 0 | 241 | | if (value is not IConvertible ic) |
| | | 242 | | { |
| | 0 | 243 | | if (value.GetType() == conversionType) |
| | | 244 | | { |
| | 0 | 245 | | return value; |
| | | 246 | | } |
| | 0 | 247 | | throw new InvalidCastException(SR.InvalidCast_IConvertible); |
| | | 248 | | } |
| | | 249 | | |
| | 0 | 250 | | if (ReferenceEquals(conversionType, typeof(bool))) |
| | 0 | 251 | | return ic.ToBoolean(provider); |
| | 0 | 252 | | if (ReferenceEquals(conversionType, typeof(char))) |
| | 0 | 253 | | return ic.ToChar(provider); |
| | 0 | 254 | | if (ReferenceEquals(conversionType, typeof(sbyte))) |
| | 0 | 255 | | return ic.ToSByte(provider); |
| | 0 | 256 | | if (ReferenceEquals(conversionType, typeof(byte))) |
| | 0 | 257 | | return ic.ToByte(provider); |
| | 0 | 258 | | if (ReferenceEquals(conversionType, typeof(short))) |
| | 0 | 259 | | return ic.ToInt16(provider); |
| | 0 | 260 | | if (ReferenceEquals(conversionType, typeof(ushort))) |
| | 0 | 261 | | return ic.ToUInt16(provider); |
| | 0 | 262 | | if (ReferenceEquals(conversionType, typeof(int))) |
| | 0 | 263 | | return ic.ToInt32(provider); |
| | 0 | 264 | | if (ReferenceEquals(conversionType, typeof(uint))) |
| | 0 | 265 | | return ic.ToUInt32(provider); |
| | 0 | 266 | | if (ReferenceEquals(conversionType, typeof(long))) |
| | 0 | 267 | | return ic.ToInt64(provider); |
| | 0 | 268 | | if (ReferenceEquals(conversionType, typeof(ulong))) |
| | 0 | 269 | | return ic.ToUInt64(provider); |
| | 0 | 270 | | if (ReferenceEquals(conversionType, typeof(float))) |
| | 0 | 271 | | return ic.ToSingle(provider); |
| | 0 | 272 | | if (ReferenceEquals(conversionType, typeof(double))) |
| | 0 | 273 | | return ic.ToDouble(provider); |
| | 0 | 274 | | if (ReferenceEquals(conversionType, typeof(decimal))) |
| | 0 | 275 | | return ic.ToDecimal(provider); |
| | 0 | 276 | | if (ReferenceEquals(conversionType, typeof(DateTime))) |
| | 0 | 277 | | return ic.ToDateTime(provider); |
| | 0 | 278 | | if (ReferenceEquals(conversionType, typeof(string))) |
| | 0 | 279 | | return ic.ToString(provider); |
| | 0 | 280 | | if (ReferenceEquals(conversionType, typeof(object))) |
| | 0 | 281 | | return (object)value; |
| | | 282 | | |
| | 0 | 283 | | return ic.ToType(conversionType, provider); |
| | | 284 | | } |
| | | 285 | | |
| | | 286 | | [DoesNotReturn] |
| | 0 | 287 | | private static void ThrowCharOverflowException() { throw new OverflowException(SR.Overflow_Char); } |
| | | 288 | | |
| | | 289 | | [DoesNotReturn] |
| | 0 | 290 | | private static void ThrowByteOverflowException() { throw new OverflowException(SR.Overflow_Byte); } |
| | | 291 | | |
| | | 292 | | [DoesNotReturn] |
| | 0 | 293 | | private static void ThrowSByteOverflowException() { throw new OverflowException(SR.Overflow_SByte); } |
| | | 294 | | |
| | | 295 | | [DoesNotReturn] |
| | 0 | 296 | | private static void ThrowInt16OverflowException() { throw new OverflowException(SR.Overflow_Int16); } |
| | | 297 | | |
| | | 298 | | [DoesNotReturn] |
| | 0 | 299 | | private static void ThrowUInt16OverflowException() { throw new OverflowException(SR.Overflow_UInt16); } |
| | | 300 | | |
| | | 301 | | [DoesNotReturn] |
| | 0 | 302 | | private static void ThrowInt32OverflowException() { throw new OverflowException(SR.Overflow_Int32); } |
| | | 303 | | |
| | | 304 | | [DoesNotReturn] |
| | 0 | 305 | | private static void ThrowUInt32OverflowException() { throw new OverflowException(SR.Overflow_UInt32); } |
| | | 306 | | |
| | | 307 | | [DoesNotReturn] |
| | 0 | 308 | | private static void ThrowInt64OverflowException() { throw new OverflowException(SR.Overflow_Int64); } |
| | | 309 | | |
| | | 310 | | [DoesNotReturn] |
| | 0 | 311 | | private static void ThrowUInt64OverflowException() { throw new OverflowException(SR.Overflow_UInt64); } |
| | | 312 | | |
| | | 313 | | // Conversions to Boolean |
| | | 314 | | public static bool ToBoolean([NotNullWhen(true)] object? value) |
| | | 315 | | { |
| | 0 | 316 | | return value != null && ((IConvertible)value).ToBoolean(null); |
| | | 317 | | } |
| | | 318 | | |
| | | 319 | | public static bool ToBoolean([NotNullWhen(true)] object? value, IFormatProvider? provider) |
| | | 320 | | { |
| | 0 | 321 | | return value != null && ((IConvertible)value).ToBoolean(provider); |
| | | 322 | | } |
| | | 323 | | |
| | | 324 | | public static bool ToBoolean(bool value) |
| | | 325 | | { |
| | 0 | 326 | | return value; |
| | | 327 | | } |
| | | 328 | | |
| | | 329 | | [CLSCompliant(false)] |
| | | 330 | | public static bool ToBoolean(sbyte value) |
| | | 331 | | { |
| | 0 | 332 | | return value != 0; |
| | | 333 | | } |
| | | 334 | | |
| | | 335 | | // To be consistent with IConvertible in the base data types else we get different semantics |
| | | 336 | | // with widening operations. Without this operator this widen succeeds,with this API the widening throws. |
| | | 337 | | public static bool ToBoolean(char value) |
| | | 338 | | { |
| | 0 | 339 | | return ((IConvertible)value).ToBoolean(null); |
| | | 340 | | } |
| | | 341 | | |
| | | 342 | | public static bool ToBoolean(byte value) |
| | | 343 | | { |
| | 0 | 344 | | return value != 0; |
| | | 345 | | } |
| | | 346 | | |
| | | 347 | | public static bool ToBoolean(short value) |
| | | 348 | | { |
| | 0 | 349 | | return value != 0; |
| | | 350 | | } |
| | | 351 | | |
| | | 352 | | [CLSCompliant(false)] |
| | | 353 | | public static bool ToBoolean(ushort value) |
| | | 354 | | { |
| | 0 | 355 | | return value != 0; |
| | | 356 | | } |
| | | 357 | | |
| | | 358 | | public static bool ToBoolean(int value) |
| | | 359 | | { |
| | 0 | 360 | | return value != 0; |
| | | 361 | | } |
| | | 362 | | |
| | | 363 | | [CLSCompliant(false)] |
| | | 364 | | public static bool ToBoolean(uint value) |
| | | 365 | | { |
| | 0 | 366 | | return value != 0; |
| | | 367 | | } |
| | | 368 | | |
| | | 369 | | public static bool ToBoolean(long value) |
| | | 370 | | { |
| | 0 | 371 | | return value != 0; |
| | | 372 | | } |
| | | 373 | | |
| | | 374 | | [CLSCompliant(false)] |
| | | 375 | | public static bool ToBoolean(ulong value) |
| | | 376 | | { |
| | 0 | 377 | | return value != 0; |
| | | 378 | | } |
| | | 379 | | |
| | | 380 | | public static bool ToBoolean([NotNullWhen(true)] string? value) |
| | | 381 | | { |
| | 0 | 382 | | if (value == null) |
| | 0 | 383 | | return false; |
| | 0 | 384 | | return bool.Parse(value); |
| | | 385 | | } |
| | | 386 | | |
| | | 387 | | public static bool ToBoolean([NotNullWhen(true)] string? value, IFormatProvider? provider) |
| | | 388 | | { |
| | 0 | 389 | | if (value == null) |
| | 0 | 390 | | return false; |
| | 0 | 391 | | return bool.Parse(value); |
| | | 392 | | } |
| | | 393 | | |
| | | 394 | | public static bool ToBoolean(float value) |
| | | 395 | | { |
| | 0 | 396 | | return value != 0; |
| | | 397 | | } |
| | | 398 | | |
| | | 399 | | public static bool ToBoolean(double value) |
| | | 400 | | { |
| | 0 | 401 | | return value != 0; |
| | | 402 | | } |
| | | 403 | | |
| | | 404 | | public static bool ToBoolean(decimal value) |
| | | 405 | | { |
| | 0 | 406 | | return value != 0; |
| | | 407 | | } |
| | | 408 | | |
| | | 409 | | public static bool ToBoolean(DateTime value) |
| | | 410 | | { |
| | 0 | 411 | | return ((IConvertible)value).ToBoolean(null); |
| | | 412 | | } |
| | | 413 | | |
| | | 414 | | // Disallowed conversions to Boolean |
| | | 415 | | // public static bool ToBoolean(TimeSpan value) |
| | | 416 | | |
| | | 417 | | // Conversions to Char |
| | | 418 | | |
| | | 419 | | public static char ToChar(object? value) |
| | | 420 | | { |
| | 0 | 421 | | return value == null ? (char)0 : ((IConvertible)value).ToChar(null); |
| | | 422 | | } |
| | | 423 | | |
| | | 424 | | public static char ToChar(object? value, IFormatProvider? provider) |
| | | 425 | | { |
| | 0 | 426 | | return value == null ? (char)0 : ((IConvertible)value).ToChar(provider); |
| | | 427 | | } |
| | | 428 | | |
| | | 429 | | public static char ToChar(bool value) |
| | | 430 | | { |
| | 0 | 431 | | return ((IConvertible)value).ToChar(null); |
| | | 432 | | } |
| | | 433 | | |
| | | 434 | | public static char ToChar(char value) |
| | | 435 | | { |
| | 0 | 436 | | return value; |
| | | 437 | | } |
| | | 438 | | |
| | | 439 | | [CLSCompliant(false)] |
| | | 440 | | public static char ToChar(sbyte value) |
| | | 441 | | { |
| | 0 | 442 | | if (value < 0) ThrowCharOverflowException(); |
| | 0 | 443 | | return (char)value; |
| | | 444 | | } |
| | | 445 | | |
| | | 446 | | public static char ToChar(byte value) |
| | | 447 | | { |
| | 0 | 448 | | return (char)value; |
| | | 449 | | } |
| | | 450 | | |
| | | 451 | | public static char ToChar(short value) |
| | | 452 | | { |
| | 0 | 453 | | if (value < 0) ThrowCharOverflowException(); |
| | 0 | 454 | | return (char)value; |
| | | 455 | | } |
| | | 456 | | |
| | | 457 | | [CLSCompliant(false)] |
| | | 458 | | public static char ToChar(ushort value) |
| | | 459 | | { |
| | 0 | 460 | | return (char)value; |
| | | 461 | | } |
| | | 462 | | |
| | 0 | 463 | | public static char ToChar(int value) => ToChar((uint)value); |
| | | 464 | | |
| | | 465 | | [CLSCompliant(false)] |
| | | 466 | | public static char ToChar(uint value) |
| | | 467 | | { |
| | 0 | 468 | | if (value > char.MaxValue) ThrowCharOverflowException(); |
| | 0 | 469 | | return (char)value; |
| | | 470 | | } |
| | | 471 | | |
| | 0 | 472 | | public static char ToChar(long value) => ToChar((ulong)value); |
| | | 473 | | |
| | | 474 | | [CLSCompliant(false)] |
| | | 475 | | public static char ToChar(ulong value) |
| | | 476 | | { |
| | 0 | 477 | | if (value > char.MaxValue) ThrowCharOverflowException(); |
| | 0 | 478 | | return (char)value; |
| | | 479 | | } |
| | | 480 | | |
| | | 481 | | // |
| | | 482 | | // @VariantSwitch |
| | | 483 | | // Remove FormatExceptions; |
| | | 484 | | // |
| | | 485 | | public static char ToChar(string value) |
| | | 486 | | { |
| | 0 | 487 | | return ToChar(value, null); |
| | | 488 | | } |
| | | 489 | | |
| | | 490 | | public static char ToChar(string value, IFormatProvider? provider) |
| | | 491 | | { |
| | 0 | 492 | | ArgumentNullException.ThrowIfNull(value); |
| | | 493 | | |
| | 0 | 494 | | if (value.Length != 1) |
| | 0 | 495 | | throw new FormatException(SR.Format_NeedSingleChar); |
| | | 496 | | |
| | 0 | 497 | | return value[0]; |
| | | 498 | | } |
| | | 499 | | |
| | | 500 | | // To be consistent with IConvertible in the base data types else we get different semantics |
| | | 501 | | // with widening operations. Without this operator this widen succeeds,with this API the widening throws. |
| | | 502 | | public static char ToChar(float value) |
| | | 503 | | { |
| | 0 | 504 | | return ((IConvertible)value).ToChar(null); |
| | | 505 | | } |
| | | 506 | | |
| | | 507 | | // To be consistent with IConvertible in the base data types else we get different semantics |
| | | 508 | | // with widening operations. Without this operator this widen succeeds,with this API the widening throws. |
| | | 509 | | public static char ToChar(double value) |
| | | 510 | | { |
| | 0 | 511 | | return ((IConvertible)value).ToChar(null); |
| | | 512 | | } |
| | | 513 | | |
| | | 514 | | // To be consistent with IConvertible in the base data types else we get different semantics |
| | | 515 | | // with widening operations. Without this operator this widen succeeds,with this API the widening throws. |
| | | 516 | | public static char ToChar(decimal value) |
| | | 517 | | { |
| | 0 | 518 | | return ((IConvertible)value).ToChar(null); |
| | | 519 | | } |
| | | 520 | | |
| | | 521 | | public static char ToChar(DateTime value) |
| | | 522 | | { |
| | 0 | 523 | | return ((IConvertible)value).ToChar(null); |
| | | 524 | | } |
| | | 525 | | |
| | | 526 | | // Disallowed conversions to Char |
| | | 527 | | // public static char ToChar(TimeSpan value) |
| | | 528 | | |
| | | 529 | | // Conversions to SByte |
| | | 530 | | |
| | | 531 | | [CLSCompliant(false)] |
| | | 532 | | public static sbyte ToSByte(object? value) |
| | | 533 | | { |
| | 0 | 534 | | return value == null ? (sbyte)0 : ((IConvertible)value).ToSByte(null); |
| | | 535 | | } |
| | | 536 | | |
| | | 537 | | [CLSCompliant(false)] |
| | | 538 | | public static sbyte ToSByte(object? value, IFormatProvider? provider) |
| | | 539 | | { |
| | 0 | 540 | | return value == null ? (sbyte)0 : ((IConvertible)value).ToSByte(provider); |
| | | 541 | | } |
| | | 542 | | |
| | | 543 | | [CLSCompliant(false)] |
| | | 544 | | public static sbyte ToSByte(bool value) |
| | | 545 | | { |
| | 0 | 546 | | return value ? (sbyte)bool.True : (sbyte)bool.False; |
| | | 547 | | } |
| | | 548 | | |
| | | 549 | | [CLSCompliant(false)] |
| | | 550 | | public static sbyte ToSByte(sbyte value) |
| | | 551 | | { |
| | 0 | 552 | | return value; |
| | | 553 | | } |
| | | 554 | | |
| | | 555 | | [CLSCompliant(false)] |
| | | 556 | | public static sbyte ToSByte(char value) |
| | | 557 | | { |
| | 0 | 558 | | if (value > sbyte.MaxValue) ThrowSByteOverflowException(); |
| | 0 | 559 | | return (sbyte)value; |
| | | 560 | | } |
| | | 561 | | |
| | | 562 | | [CLSCompliant(false)] |
| | | 563 | | public static sbyte ToSByte(byte value) |
| | | 564 | | { |
| | 0 | 565 | | if (value > sbyte.MaxValue) ThrowSByteOverflowException(); |
| | 0 | 566 | | return (sbyte)value; |
| | | 567 | | } |
| | | 568 | | |
| | | 569 | | [CLSCompliant(false)] |
| | | 570 | | public static sbyte ToSByte(short value) |
| | | 571 | | { |
| | 0 | 572 | | if (value < sbyte.MinValue || value > sbyte.MaxValue) ThrowSByteOverflowException(); |
| | 0 | 573 | | return (sbyte)value; |
| | | 574 | | } |
| | | 575 | | |
| | | 576 | | [CLSCompliant(false)] |
| | | 577 | | public static sbyte ToSByte(ushort value) |
| | | 578 | | { |
| | 0 | 579 | | if (value > sbyte.MaxValue) ThrowSByteOverflowException(); |
| | 0 | 580 | | return (sbyte)value; |
| | | 581 | | } |
| | | 582 | | |
| | | 583 | | [CLSCompliant(false)] |
| | | 584 | | public static sbyte ToSByte(int value) |
| | | 585 | | { |
| | 0 | 586 | | if (value < sbyte.MinValue || value > sbyte.MaxValue) ThrowSByteOverflowException(); |
| | 0 | 587 | | return (sbyte)value; |
| | | 588 | | } |
| | | 589 | | |
| | | 590 | | [CLSCompliant(false)] |
| | | 591 | | public static sbyte ToSByte(uint value) |
| | | 592 | | { |
| | 0 | 593 | | if (value > (uint)sbyte.MaxValue) ThrowSByteOverflowException(); |
| | 0 | 594 | | return (sbyte)value; |
| | | 595 | | } |
| | | 596 | | |
| | | 597 | | [CLSCompliant(false)] |
| | | 598 | | public static sbyte ToSByte(long value) |
| | | 599 | | { |
| | 0 | 600 | | if (value < sbyte.MinValue || value > sbyte.MaxValue) ThrowSByteOverflowException(); |
| | 0 | 601 | | return (sbyte)value; |
| | | 602 | | } |
| | | 603 | | |
| | | 604 | | [CLSCompliant(false)] |
| | | 605 | | public static sbyte ToSByte(ulong value) |
| | | 606 | | { |
| | 0 | 607 | | if (value > (ulong)sbyte.MaxValue) ThrowSByteOverflowException(); |
| | 0 | 608 | | return (sbyte)value; |
| | | 609 | | } |
| | | 610 | | |
| | | 611 | | [CLSCompliant(false)] |
| | | 612 | | public static sbyte ToSByte(float value) |
| | | 613 | | { |
| | 0 | 614 | | return ToSByte((double)value); |
| | | 615 | | } |
| | | 616 | | |
| | | 617 | | [CLSCompliant(false)] |
| | | 618 | | public static sbyte ToSByte(double value) |
| | | 619 | | { |
| | 0 | 620 | | return ToSByte(ToInt32(value)); |
| | | 621 | | } |
| | | 622 | | |
| | | 623 | | [CLSCompliant(false)] |
| | | 624 | | public static sbyte ToSByte(decimal value) |
| | | 625 | | { |
| | 0 | 626 | | return decimal.ToSByte(decimal.Round(value, 0)); |
| | | 627 | | } |
| | | 628 | | |
| | | 629 | | [CLSCompliant(false)] |
| | | 630 | | public static sbyte ToSByte(string? value) |
| | | 631 | | { |
| | 0 | 632 | | if (value == null) |
| | 0 | 633 | | return 0; |
| | 0 | 634 | | return sbyte.Parse(value); |
| | | 635 | | } |
| | | 636 | | |
| | | 637 | | [CLSCompliant(false)] |
| | | 638 | | public static sbyte ToSByte(string value, IFormatProvider? provider) |
| | | 639 | | { |
| | 0 | 640 | | return sbyte.Parse(value, provider); |
| | | 641 | | } |
| | | 642 | | |
| | | 643 | | [CLSCompliant(false)] |
| | | 644 | | public static sbyte ToSByte(DateTime value) |
| | | 645 | | { |
| | 0 | 646 | | return ((IConvertible)value).ToSByte(null); |
| | | 647 | | } |
| | | 648 | | |
| | | 649 | | // Disallowed conversions to SByte |
| | | 650 | | // public static sbyte ToSByte(TimeSpan value) |
| | | 651 | | |
| | | 652 | | // Conversions to Byte |
| | | 653 | | |
| | | 654 | | public static byte ToByte(object? value) |
| | | 655 | | { |
| | 0 | 656 | | return value == null ? (byte)0 : ((IConvertible)value).ToByte(null); |
| | | 657 | | } |
| | | 658 | | |
| | | 659 | | public static byte ToByte(object? value, IFormatProvider? provider) |
| | | 660 | | { |
| | 0 | 661 | | return value == null ? (byte)0 : ((IConvertible)value).ToByte(provider); |
| | | 662 | | } |
| | | 663 | | |
| | | 664 | | public static byte ToByte(bool value) |
| | | 665 | | { |
| | 0 | 666 | | return value ? (byte)bool.True : (byte)bool.False; |
| | | 667 | | } |
| | | 668 | | |
| | | 669 | | public static byte ToByte(byte value) |
| | | 670 | | { |
| | 0 | 671 | | return value; |
| | | 672 | | } |
| | | 673 | | |
| | | 674 | | public static byte ToByte(char value) |
| | | 675 | | { |
| | 0 | 676 | | if (value > byte.MaxValue) ThrowByteOverflowException(); |
| | 0 | 677 | | return (byte)value; |
| | | 678 | | } |
| | | 679 | | |
| | | 680 | | [CLSCompliant(false)] |
| | | 681 | | public static byte ToByte(sbyte value) |
| | | 682 | | { |
| | 0 | 683 | | if (value < 0) ThrowByteOverflowException(); |
| | 0 | 684 | | return (byte)value; |
| | | 685 | | } |
| | | 686 | | |
| | | 687 | | public static byte ToByte(short value) |
| | | 688 | | { |
| | 0 | 689 | | if ((uint)value > byte.MaxValue) ThrowByteOverflowException(); |
| | 0 | 690 | | return (byte)value; |
| | | 691 | | } |
| | | 692 | | |
| | | 693 | | [CLSCompliant(false)] |
| | | 694 | | public static byte ToByte(ushort value) |
| | | 695 | | { |
| | 0 | 696 | | if (value > byte.MaxValue) ThrowByteOverflowException(); |
| | 0 | 697 | | return (byte)value; |
| | | 698 | | } |
| | | 699 | | |
| | 0 | 700 | | public static byte ToByte(int value) => ToByte((uint)value); |
| | | 701 | | |
| | | 702 | | [CLSCompliant(false)] |
| | | 703 | | public static byte ToByte(uint value) |
| | | 704 | | { |
| | 0 | 705 | | if (value > byte.MaxValue) ThrowByteOverflowException(); |
| | 0 | 706 | | return (byte)value; |
| | | 707 | | } |
| | | 708 | | |
| | 0 | 709 | | public static byte ToByte(long value) => ToByte((ulong)value); |
| | | 710 | | |
| | | 711 | | [CLSCompliant(false)] |
| | | 712 | | public static byte ToByte(ulong value) |
| | | 713 | | { |
| | 0 | 714 | | if (value > byte.MaxValue) ThrowByteOverflowException(); |
| | 0 | 715 | | return (byte)value; |
| | | 716 | | } |
| | | 717 | | |
| | | 718 | | public static byte ToByte(float value) |
| | | 719 | | { |
| | 0 | 720 | | return ToByte((double)value); |
| | | 721 | | } |
| | | 722 | | |
| | | 723 | | public static byte ToByte(double value) |
| | | 724 | | { |
| | 0 | 725 | | return ToByte(ToInt32(value)); |
| | | 726 | | } |
| | | 727 | | |
| | | 728 | | public static byte ToByte(decimal value) |
| | | 729 | | { |
| | 0 | 730 | | return decimal.ToByte(decimal.Round(value, 0)); |
| | | 731 | | } |
| | | 732 | | |
| | | 733 | | public static byte ToByte(string? value) |
| | | 734 | | { |
| | 0 | 735 | | if (value == null) |
| | 0 | 736 | | return 0; |
| | 0 | 737 | | return byte.Parse(value); |
| | | 738 | | } |
| | | 739 | | |
| | | 740 | | public static byte ToByte(string? value, IFormatProvider? provider) |
| | | 741 | | { |
| | 0 | 742 | | if (value == null) |
| | 0 | 743 | | return 0; |
| | 0 | 744 | | return byte.Parse(value, provider); |
| | | 745 | | } |
| | | 746 | | |
| | | 747 | | public static byte ToByte(DateTime value) |
| | | 748 | | { |
| | 0 | 749 | | return ((IConvertible)value).ToByte(null); |
| | | 750 | | } |
| | | 751 | | |
| | | 752 | | // Disallowed conversions to Byte |
| | | 753 | | // public static byte ToByte(TimeSpan value) |
| | | 754 | | |
| | | 755 | | // Conversions to Int16 |
| | | 756 | | |
| | | 757 | | public static short ToInt16(object? value) |
| | | 758 | | { |
| | 0 | 759 | | return value == null ? (short)0 : ((IConvertible)value).ToInt16(null); |
| | | 760 | | } |
| | | 761 | | |
| | | 762 | | public static short ToInt16(object? value, IFormatProvider? provider) |
| | | 763 | | { |
| | 0 | 764 | | return value == null ? (short)0 : ((IConvertible)value).ToInt16(provider); |
| | | 765 | | } |
| | | 766 | | |
| | | 767 | | public static short ToInt16(bool value) |
| | | 768 | | { |
| | 0 | 769 | | return value ? (short)bool.True : (short)bool.False; |
| | | 770 | | } |
| | | 771 | | |
| | | 772 | | public static short ToInt16(char value) |
| | | 773 | | { |
| | 0 | 774 | | if (value > short.MaxValue) ThrowInt16OverflowException(); |
| | 0 | 775 | | return (short)value; |
| | | 776 | | } |
| | | 777 | | |
| | | 778 | | [CLSCompliant(false)] |
| | | 779 | | public static short ToInt16(sbyte value) |
| | | 780 | | { |
| | 0 | 781 | | return value; |
| | | 782 | | } |
| | | 783 | | |
| | | 784 | | public static short ToInt16(byte value) |
| | | 785 | | { |
| | 0 | 786 | | return value; |
| | | 787 | | } |
| | | 788 | | |
| | | 789 | | [CLSCompliant(false)] |
| | | 790 | | public static short ToInt16(ushort value) |
| | | 791 | | { |
| | 0 | 792 | | if (value > short.MaxValue) ThrowInt16OverflowException(); |
| | 0 | 793 | | return (short)value; |
| | | 794 | | } |
| | | 795 | | |
| | | 796 | | public static short ToInt16(int value) |
| | | 797 | | { |
| | 0 | 798 | | if (value < short.MinValue || value > short.MaxValue) ThrowInt16OverflowException(); |
| | 0 | 799 | | return (short)value; |
| | | 800 | | } |
| | | 801 | | |
| | | 802 | | [CLSCompliant(false)] |
| | | 803 | | public static short ToInt16(uint value) |
| | | 804 | | { |
| | 0 | 805 | | if (value > (uint)short.MaxValue) ThrowInt16OverflowException(); |
| | 0 | 806 | | return (short)value; |
| | | 807 | | } |
| | | 808 | | |
| | | 809 | | public static short ToInt16(short value) |
| | | 810 | | { |
| | 0 | 811 | | return value; |
| | | 812 | | } |
| | | 813 | | |
| | | 814 | | public static short ToInt16(long value) |
| | | 815 | | { |
| | 0 | 816 | | if (value < short.MinValue || value > short.MaxValue) ThrowInt16OverflowException(); |
| | 0 | 817 | | return (short)value; |
| | | 818 | | } |
| | | 819 | | |
| | | 820 | | [CLSCompliant(false)] |
| | | 821 | | public static short ToInt16(ulong value) |
| | | 822 | | { |
| | 0 | 823 | | if (value > (ulong)short.MaxValue) ThrowInt16OverflowException(); |
| | 0 | 824 | | return (short)value; |
| | | 825 | | } |
| | | 826 | | |
| | | 827 | | public static short ToInt16(float value) |
| | | 828 | | { |
| | 0 | 829 | | return ToInt16((double)value); |
| | | 830 | | } |
| | | 831 | | |
| | | 832 | | public static short ToInt16(double value) |
| | | 833 | | { |
| | 0 | 834 | | return ToInt16(ToInt32(value)); |
| | | 835 | | } |
| | | 836 | | |
| | | 837 | | public static short ToInt16(decimal value) |
| | | 838 | | { |
| | 0 | 839 | | return decimal.ToInt16(decimal.Round(value, 0)); |
| | | 840 | | } |
| | | 841 | | |
| | | 842 | | public static short ToInt16(string? value) |
| | | 843 | | { |
| | 0 | 844 | | if (value == null) |
| | 0 | 845 | | return 0; |
| | 0 | 846 | | return short.Parse(value); |
| | | 847 | | } |
| | | 848 | | |
| | | 849 | | public static short ToInt16(string? value, IFormatProvider? provider) |
| | | 850 | | { |
| | 0 | 851 | | if (value == null) |
| | 0 | 852 | | return 0; |
| | 0 | 853 | | return short.Parse(value, provider); |
| | | 854 | | } |
| | | 855 | | |
| | | 856 | | public static short ToInt16(DateTime value) |
| | | 857 | | { |
| | 0 | 858 | | return ((IConvertible)value).ToInt16(null); |
| | | 859 | | } |
| | | 860 | | |
| | | 861 | | // Disallowed conversions to Int16 |
| | | 862 | | // public static short ToInt16(TimeSpan value) |
| | | 863 | | |
| | | 864 | | // Conversions to UInt16 |
| | | 865 | | |
| | | 866 | | [CLSCompliant(false)] |
| | | 867 | | public static ushort ToUInt16(object? value) |
| | | 868 | | { |
| | 0 | 869 | | return value == null ? (ushort)0 : ((IConvertible)value).ToUInt16(null); |
| | | 870 | | } |
| | | 871 | | |
| | | 872 | | [CLSCompliant(false)] |
| | | 873 | | public static ushort ToUInt16(object? value, IFormatProvider? provider) |
| | | 874 | | { |
| | 0 | 875 | | return value == null ? (ushort)0 : ((IConvertible)value).ToUInt16(provider); |
| | | 876 | | } |
| | | 877 | | |
| | | 878 | | [CLSCompliant(false)] |
| | | 879 | | public static ushort ToUInt16(bool value) |
| | | 880 | | { |
| | 0 | 881 | | return value ? (ushort)bool.True : (ushort)bool.False; |
| | | 882 | | } |
| | | 883 | | |
| | | 884 | | [CLSCompliant(false)] |
| | | 885 | | public static ushort ToUInt16(char value) |
| | | 886 | | { |
| | 0 | 887 | | return value; |
| | | 888 | | } |
| | | 889 | | |
| | | 890 | | [CLSCompliant(false)] |
| | | 891 | | public static ushort ToUInt16(sbyte value) |
| | | 892 | | { |
| | 0 | 893 | | if (value < 0) ThrowUInt16OverflowException(); |
| | 0 | 894 | | return (ushort)value; |
| | | 895 | | } |
| | | 896 | | |
| | | 897 | | [CLSCompliant(false)] |
| | | 898 | | public static ushort ToUInt16(byte value) |
| | | 899 | | { |
| | 0 | 900 | | return value; |
| | | 901 | | } |
| | | 902 | | |
| | | 903 | | [CLSCompliant(false)] |
| | | 904 | | public static ushort ToUInt16(short value) |
| | | 905 | | { |
| | 0 | 906 | | if (value < 0) ThrowUInt16OverflowException(); |
| | 0 | 907 | | return (ushort)value; |
| | | 908 | | } |
| | | 909 | | |
| | | 910 | | [CLSCompliant(false)] |
| | 0 | 911 | | public static ushort ToUInt16(int value) => ToUInt16((uint)value); |
| | | 912 | | |
| | | 913 | | [CLSCompliant(false)] |
| | | 914 | | public static ushort ToUInt16(ushort value) |
| | | 915 | | { |
| | 0 | 916 | | return value; |
| | | 917 | | } |
| | | 918 | | |
| | | 919 | | [CLSCompliant(false)] |
| | | 920 | | public static ushort ToUInt16(uint value) |
| | | 921 | | { |
| | 0 | 922 | | if (value > ushort.MaxValue) ThrowUInt16OverflowException(); |
| | 0 | 923 | | return (ushort)value; |
| | | 924 | | } |
| | | 925 | | |
| | | 926 | | [CLSCompliant(false)] |
| | 0 | 927 | | public static ushort ToUInt16(long value) => ToUInt16((ulong)value); |
| | | 928 | | |
| | | 929 | | [CLSCompliant(false)] |
| | | 930 | | public static ushort ToUInt16(ulong value) |
| | | 931 | | { |
| | 0 | 932 | | if (value > ushort.MaxValue) ThrowUInt16OverflowException(); |
| | 0 | 933 | | return (ushort)value; |
| | | 934 | | } |
| | | 935 | | |
| | | 936 | | [CLSCompliant(false)] |
| | | 937 | | public static ushort ToUInt16(float value) |
| | | 938 | | { |
| | 0 | 939 | | return ToUInt16((double)value); |
| | | 940 | | } |
| | | 941 | | |
| | | 942 | | [CLSCompliant(false)] |
| | | 943 | | public static ushort ToUInt16(double value) |
| | | 944 | | { |
| | 0 | 945 | | return ToUInt16(ToInt32(value)); |
| | | 946 | | } |
| | | 947 | | |
| | | 948 | | [CLSCompliant(false)] |
| | | 949 | | public static ushort ToUInt16(decimal value) |
| | | 950 | | { |
| | 0 | 951 | | return decimal.ToUInt16(decimal.Round(value, 0)); |
| | | 952 | | } |
| | | 953 | | |
| | | 954 | | [CLSCompliant(false)] |
| | | 955 | | public static ushort ToUInt16(string? value) |
| | | 956 | | { |
| | 0 | 957 | | if (value == null) |
| | 0 | 958 | | return 0; |
| | 0 | 959 | | return ushort.Parse(value); |
| | | 960 | | } |
| | | 961 | | |
| | | 962 | | [CLSCompliant(false)] |
| | | 963 | | public static ushort ToUInt16(string? value, IFormatProvider? provider) |
| | | 964 | | { |
| | 0 | 965 | | if (value == null) |
| | 0 | 966 | | return 0; |
| | 0 | 967 | | return ushort.Parse(value, provider); |
| | | 968 | | } |
| | | 969 | | |
| | | 970 | | [CLSCompliant(false)] |
| | | 971 | | public static ushort ToUInt16(DateTime value) |
| | | 972 | | { |
| | 0 | 973 | | return ((IConvertible)value).ToUInt16(null); |
| | | 974 | | } |
| | | 975 | | |
| | | 976 | | // Disallowed conversions to UInt16 |
| | | 977 | | // public static ushort ToUInt16(TimeSpan value) |
| | | 978 | | |
| | | 979 | | // Conversions to Int32 |
| | | 980 | | |
| | | 981 | | public static int ToInt32(object? value) |
| | | 982 | | { |
| | 0 | 983 | | return value == null ? 0 : ((IConvertible)value).ToInt32(null); |
| | | 984 | | } |
| | | 985 | | |
| | | 986 | | public static int ToInt32(object? value, IFormatProvider? provider) |
| | | 987 | | { |
| | 0 | 988 | | return value == null ? 0 : ((IConvertible)value).ToInt32(provider); |
| | | 989 | | } |
| | | 990 | | |
| | | 991 | | public static int ToInt32(bool value) |
| | | 992 | | { |
| | 0 | 993 | | return value ? bool.True : bool.False; |
| | | 994 | | } |
| | | 995 | | |
| | | 996 | | public static int ToInt32(char value) |
| | | 997 | | { |
| | 0 | 998 | | return value; |
| | | 999 | | } |
| | | 1000 | | |
| | | 1001 | | [CLSCompliant(false)] |
| | | 1002 | | public static int ToInt32(sbyte value) |
| | | 1003 | | { |
| | 0 | 1004 | | return value; |
| | | 1005 | | } |
| | | 1006 | | |
| | | 1007 | | public static int ToInt32(byte value) |
| | | 1008 | | { |
| | 0 | 1009 | | return value; |
| | | 1010 | | } |
| | | 1011 | | |
| | | 1012 | | public static int ToInt32(short value) |
| | | 1013 | | { |
| | 0 | 1014 | | return value; |
| | | 1015 | | } |
| | | 1016 | | |
| | | 1017 | | [CLSCompliant(false)] |
| | | 1018 | | public static int ToInt32(ushort value) |
| | | 1019 | | { |
| | 0 | 1020 | | return value; |
| | | 1021 | | } |
| | | 1022 | | |
| | | 1023 | | [CLSCompliant(false)] |
| | | 1024 | | public static int ToInt32(uint value) |
| | | 1025 | | { |
| | 0 | 1026 | | if ((int)value < 0) ThrowInt32OverflowException(); |
| | 0 | 1027 | | return (int)value; |
| | | 1028 | | } |
| | | 1029 | | |
| | | 1030 | | public static int ToInt32(int value) |
| | | 1031 | | { |
| | 0 | 1032 | | return value; |
| | | 1033 | | } |
| | | 1034 | | |
| | | 1035 | | public static int ToInt32(long value) |
| | | 1036 | | { |
| | 0 | 1037 | | if (value < int.MinValue || value > int.MaxValue) ThrowInt32OverflowException(); |
| | 0 | 1038 | | return (int)value; |
| | | 1039 | | } |
| | | 1040 | | |
| | | 1041 | | [CLSCompliant(false)] |
| | | 1042 | | public static int ToInt32(ulong value) |
| | | 1043 | | { |
| | 0 | 1044 | | if (value > int.MaxValue) ThrowInt32OverflowException(); |
| | 0 | 1045 | | return (int)value; |
| | | 1046 | | } |
| | | 1047 | | |
| | | 1048 | | public static int ToInt32(float value) |
| | | 1049 | | { |
| | 0 | 1050 | | return ToInt32((double)value); |
| | | 1051 | | } |
| | | 1052 | | |
| | | 1053 | | public static int ToInt32(double value) |
| | | 1054 | | { |
| | 0 | 1055 | | if (value >= 0) |
| | | 1056 | | { |
| | 0 | 1057 | | if (value < 2147483647.5) |
| | | 1058 | | { |
| | 0 | 1059 | | int result = (int)value; |
| | 0 | 1060 | | double dif = value - result; |
| | 0 | 1061 | | if (dif > 0.5 || dif == 0.5 && (result & 1) != 0) result++; |
| | 0 | 1062 | | return result; |
| | | 1063 | | } |
| | | 1064 | | } |
| | | 1065 | | else |
| | | 1066 | | { |
| | 0 | 1067 | | if (value >= -2147483648.5) |
| | | 1068 | | { |
| | 0 | 1069 | | int result = (int)value; |
| | 0 | 1070 | | double dif = value - result; |
| | 0 | 1071 | | if (dif < -0.5 || dif == -0.5 && (result & 1) != 0) result--; |
| | 0 | 1072 | | return result; |
| | | 1073 | | } |
| | | 1074 | | } |
| | 0 | 1075 | | throw new OverflowException(SR.Overflow_Int32); |
| | | 1076 | | } |
| | | 1077 | | |
| | | 1078 | | public static int ToInt32(decimal value) |
| | | 1079 | | { |
| | 0 | 1080 | | return decimal.ToInt32(decimal.Round(value, 0)); |
| | | 1081 | | } |
| | | 1082 | | |
| | | 1083 | | public static int ToInt32(string? value) |
| | | 1084 | | { |
| | 0 | 1085 | | if (value == null) |
| | 0 | 1086 | | return 0; |
| | 0 | 1087 | | return int.Parse(value); |
| | | 1088 | | } |
| | | 1089 | | |
| | | 1090 | | public static int ToInt32(string? value, IFormatProvider? provider) |
| | | 1091 | | { |
| | 0 | 1092 | | if (value == null) |
| | 0 | 1093 | | return 0; |
| | 0 | 1094 | | return int.Parse(value, provider); |
| | | 1095 | | } |
| | | 1096 | | |
| | | 1097 | | public static int ToInt32(DateTime value) |
| | | 1098 | | { |
| | 0 | 1099 | | return ((IConvertible)value).ToInt32(null); |
| | | 1100 | | } |
| | | 1101 | | |
| | | 1102 | | // Disallowed conversions to Int32 |
| | | 1103 | | // public static int ToInt32(TimeSpan value) |
| | | 1104 | | |
| | | 1105 | | // Conversions to UInt32 |
| | | 1106 | | |
| | | 1107 | | [CLSCompliant(false)] |
| | | 1108 | | public static uint ToUInt32(object? value) |
| | | 1109 | | { |
| | 0 | 1110 | | return value == null ? 0 : ((IConvertible)value).ToUInt32(null); |
| | | 1111 | | } |
| | | 1112 | | |
| | | 1113 | | [CLSCompliant(false)] |
| | | 1114 | | public static uint ToUInt32(object? value, IFormatProvider? provider) |
| | | 1115 | | { |
| | 0 | 1116 | | return value == null ? 0 : ((IConvertible)value).ToUInt32(provider); |
| | | 1117 | | } |
| | | 1118 | | |
| | | 1119 | | [CLSCompliant(false)] |
| | | 1120 | | public static uint ToUInt32(bool value) |
| | | 1121 | | { |
| | 0 | 1122 | | return value ? (uint)bool.True : (uint)bool.False; |
| | | 1123 | | } |
| | | 1124 | | |
| | | 1125 | | [CLSCompliant(false)] |
| | | 1126 | | public static uint ToUInt32(char value) |
| | | 1127 | | { |
| | 0 | 1128 | | return value; |
| | | 1129 | | } |
| | | 1130 | | |
| | | 1131 | | [CLSCompliant(false)] |
| | | 1132 | | public static uint ToUInt32(sbyte value) |
| | | 1133 | | { |
| | 0 | 1134 | | if (value < 0) ThrowUInt32OverflowException(); |
| | 0 | 1135 | | return (uint)value; |
| | | 1136 | | } |
| | | 1137 | | |
| | | 1138 | | [CLSCompliant(false)] |
| | | 1139 | | public static uint ToUInt32(byte value) |
| | | 1140 | | { |
| | 0 | 1141 | | return value; |
| | | 1142 | | } |
| | | 1143 | | |
| | | 1144 | | [CLSCompliant(false)] |
| | | 1145 | | public static uint ToUInt32(short value) |
| | | 1146 | | { |
| | 0 | 1147 | | if (value < 0) ThrowUInt32OverflowException(); |
| | 0 | 1148 | | return (uint)value; |
| | | 1149 | | } |
| | | 1150 | | |
| | | 1151 | | [CLSCompliant(false)] |
| | | 1152 | | public static uint ToUInt32(ushort value) |
| | | 1153 | | { |
| | 0 | 1154 | | return value; |
| | | 1155 | | } |
| | | 1156 | | |
| | | 1157 | | [CLSCompliant(false)] |
| | | 1158 | | public static uint ToUInt32(int value) |
| | | 1159 | | { |
| | 0 | 1160 | | if (value < 0) ThrowUInt32OverflowException(); |
| | 0 | 1161 | | return (uint)value; |
| | | 1162 | | } |
| | | 1163 | | |
| | | 1164 | | [CLSCompliant(false)] |
| | | 1165 | | public static uint ToUInt32(uint value) |
| | | 1166 | | { |
| | 0 | 1167 | | return value; |
| | | 1168 | | } |
| | | 1169 | | |
| | | 1170 | | [CLSCompliant(false)] |
| | 0 | 1171 | | public static uint ToUInt32(long value) => ToUInt32((ulong)value); |
| | | 1172 | | |
| | | 1173 | | [CLSCompliant(false)] |
| | | 1174 | | public static uint ToUInt32(ulong value) |
| | | 1175 | | { |
| | 0 | 1176 | | if (value > uint.MaxValue) ThrowUInt32OverflowException(); |
| | 0 | 1177 | | return (uint)value; |
| | | 1178 | | } |
| | | 1179 | | |
| | | 1180 | | [CLSCompliant(false)] |
| | | 1181 | | public static uint ToUInt32(float value) |
| | | 1182 | | { |
| | 0 | 1183 | | return ToUInt32((double)value); |
| | | 1184 | | } |
| | | 1185 | | |
| | | 1186 | | [CLSCompliant(false)] |
| | | 1187 | | public static uint ToUInt32(double value) |
| | | 1188 | | { |
| | 0 | 1189 | | if (value >= -0.5 && value < 4294967295.5) |
| | | 1190 | | { |
| | 0 | 1191 | | uint result = (uint)value; |
| | 0 | 1192 | | double dif = value - result; |
| | 0 | 1193 | | if (dif > 0.5 || dif == 0.5 && (result & 1) != 0) result++; |
| | 0 | 1194 | | return result; |
| | | 1195 | | } |
| | 0 | 1196 | | throw new OverflowException(SR.Overflow_UInt32); |
| | | 1197 | | } |
| | | 1198 | | |
| | | 1199 | | [CLSCompliant(false)] |
| | | 1200 | | public static uint ToUInt32(decimal value) |
| | | 1201 | | { |
| | 0 | 1202 | | return decimal.ToUInt32(decimal.Round(value, 0)); |
| | | 1203 | | } |
| | | 1204 | | |
| | | 1205 | | [CLSCompliant(false)] |
| | | 1206 | | public static uint ToUInt32(string? value) |
| | | 1207 | | { |
| | 0 | 1208 | | if (value == null) |
| | 0 | 1209 | | return 0; |
| | 0 | 1210 | | return uint.Parse(value); |
| | | 1211 | | } |
| | | 1212 | | |
| | | 1213 | | [CLSCompliant(false)] |
| | | 1214 | | public static uint ToUInt32(string? value, IFormatProvider? provider) |
| | | 1215 | | { |
| | 0 | 1216 | | if (value == null) |
| | 0 | 1217 | | return 0; |
| | 0 | 1218 | | return uint.Parse(value, provider); |
| | | 1219 | | } |
| | | 1220 | | |
| | | 1221 | | [CLSCompliant(false)] |
| | | 1222 | | public static uint ToUInt32(DateTime value) |
| | | 1223 | | { |
| | 0 | 1224 | | return ((IConvertible)value).ToUInt32(null); |
| | | 1225 | | } |
| | | 1226 | | |
| | | 1227 | | // Disallowed conversions to UInt32 |
| | | 1228 | | // public static uint ToUInt32(TimeSpan value) |
| | | 1229 | | |
| | | 1230 | | // Conversions to Int64 |
| | | 1231 | | |
| | | 1232 | | public static long ToInt64(object? value) |
| | | 1233 | | { |
| | 0 | 1234 | | return value == null ? 0 : ((IConvertible)value).ToInt64(null); |
| | | 1235 | | } |
| | | 1236 | | |
| | | 1237 | | public static long ToInt64(object? value, IFormatProvider? provider) |
| | | 1238 | | { |
| | 0 | 1239 | | return value == null ? 0 : ((IConvertible)value).ToInt64(provider); |
| | | 1240 | | } |
| | | 1241 | | |
| | | 1242 | | public static long ToInt64(bool value) |
| | | 1243 | | { |
| | 0 | 1244 | | return value ? bool.True : bool.False; |
| | | 1245 | | } |
| | | 1246 | | |
| | | 1247 | | public static long ToInt64(char value) |
| | | 1248 | | { |
| | 0 | 1249 | | return value; |
| | | 1250 | | } |
| | | 1251 | | |
| | | 1252 | | [CLSCompliant(false)] |
| | | 1253 | | public static long ToInt64(sbyte value) |
| | | 1254 | | { |
| | 0 | 1255 | | return value; |
| | | 1256 | | } |
| | | 1257 | | |
| | | 1258 | | public static long ToInt64(byte value) |
| | | 1259 | | { |
| | 0 | 1260 | | return value; |
| | | 1261 | | } |
| | | 1262 | | |
| | | 1263 | | public static long ToInt64(short value) |
| | | 1264 | | { |
| | 0 | 1265 | | return value; |
| | | 1266 | | } |
| | | 1267 | | |
| | | 1268 | | [CLSCompliant(false)] |
| | | 1269 | | public static long ToInt64(ushort value) |
| | | 1270 | | { |
| | 0 | 1271 | | return value; |
| | | 1272 | | } |
| | | 1273 | | |
| | | 1274 | | public static long ToInt64(int value) |
| | | 1275 | | { |
| | 0 | 1276 | | return value; |
| | | 1277 | | } |
| | | 1278 | | |
| | | 1279 | | [CLSCompliant(false)] |
| | | 1280 | | public static long ToInt64(uint value) |
| | | 1281 | | { |
| | 0 | 1282 | | return value; |
| | | 1283 | | } |
| | | 1284 | | |
| | | 1285 | | [CLSCompliant(false)] |
| | | 1286 | | public static long ToInt64(ulong value) |
| | | 1287 | | { |
| | 0 | 1288 | | if ((long)value < 0) ThrowInt64OverflowException(); |
| | 0 | 1289 | | return (long)value; |
| | | 1290 | | } |
| | | 1291 | | |
| | | 1292 | | public static long ToInt64(long value) |
| | | 1293 | | { |
| | 0 | 1294 | | return value; |
| | | 1295 | | } |
| | | 1296 | | |
| | | 1297 | | public static long ToInt64(float value) |
| | | 1298 | | { |
| | 0 | 1299 | | return ToInt64((double)value); |
| | | 1300 | | } |
| | | 1301 | | |
| | | 1302 | | public static long ToInt64(double value) |
| | | 1303 | | { |
| | 0 | 1304 | | return checked((long)Math.Round(value)); |
| | | 1305 | | } |
| | | 1306 | | |
| | | 1307 | | public static long ToInt64(decimal value) |
| | | 1308 | | { |
| | 0 | 1309 | | return decimal.ToInt64(decimal.Round(value, 0)); |
| | | 1310 | | } |
| | | 1311 | | |
| | | 1312 | | public static long ToInt64(string? value) |
| | | 1313 | | { |
| | 0 | 1314 | | if (value == null) |
| | 0 | 1315 | | return 0; |
| | 0 | 1316 | | return long.Parse(value); |
| | | 1317 | | } |
| | | 1318 | | |
| | | 1319 | | public static long ToInt64(string? value, IFormatProvider? provider) |
| | | 1320 | | { |
| | 0 | 1321 | | if (value == null) |
| | 0 | 1322 | | return 0; |
| | 0 | 1323 | | return long.Parse(value, provider); |
| | | 1324 | | } |
| | | 1325 | | |
| | | 1326 | | public static long ToInt64(DateTime value) |
| | | 1327 | | { |
| | 0 | 1328 | | return ((IConvertible)value).ToInt64(null); |
| | | 1329 | | } |
| | | 1330 | | |
| | | 1331 | | // Disallowed conversions to Int64 |
| | | 1332 | | // public static long ToInt64(TimeSpan value) |
| | | 1333 | | |
| | | 1334 | | // Conversions to UInt64 |
| | | 1335 | | |
| | | 1336 | | [CLSCompliant(false)] |
| | | 1337 | | public static ulong ToUInt64(object? value) |
| | | 1338 | | { |
| | 0 | 1339 | | return value == null ? 0 : ((IConvertible)value).ToUInt64(null); |
| | | 1340 | | } |
| | | 1341 | | |
| | | 1342 | | [CLSCompliant(false)] |
| | | 1343 | | public static ulong ToUInt64(object? value, IFormatProvider? provider) |
| | | 1344 | | { |
| | 0 | 1345 | | return value == null ? 0 : ((IConvertible)value).ToUInt64(provider); |
| | | 1346 | | } |
| | | 1347 | | |
| | | 1348 | | [CLSCompliant(false)] |
| | | 1349 | | public static ulong ToUInt64(bool value) |
| | | 1350 | | { |
| | 0 | 1351 | | return value ? (ulong)bool.True : (ulong)bool.False; |
| | | 1352 | | } |
| | | 1353 | | |
| | | 1354 | | [CLSCompliant(false)] |
| | | 1355 | | public static ulong ToUInt64(char value) |
| | | 1356 | | { |
| | 0 | 1357 | | return value; |
| | | 1358 | | } |
| | | 1359 | | |
| | | 1360 | | [CLSCompliant(false)] |
| | | 1361 | | public static ulong ToUInt64(sbyte value) |
| | | 1362 | | { |
| | 0 | 1363 | | if (value < 0) ThrowUInt64OverflowException(); |
| | 0 | 1364 | | return (ulong)value; |
| | | 1365 | | } |
| | | 1366 | | |
| | | 1367 | | [CLSCompliant(false)] |
| | | 1368 | | public static ulong ToUInt64(byte value) |
| | | 1369 | | { |
| | 0 | 1370 | | return value; |
| | | 1371 | | } |
| | | 1372 | | |
| | | 1373 | | [CLSCompliant(false)] |
| | | 1374 | | public static ulong ToUInt64(short value) |
| | | 1375 | | { |
| | 0 | 1376 | | if (value < 0) ThrowUInt64OverflowException(); |
| | 0 | 1377 | | return (ulong)value; |
| | | 1378 | | } |
| | | 1379 | | |
| | | 1380 | | [CLSCompliant(false)] |
| | | 1381 | | public static ulong ToUInt64(ushort value) |
| | | 1382 | | { |
| | 0 | 1383 | | return value; |
| | | 1384 | | } |
| | | 1385 | | |
| | | 1386 | | [CLSCompliant(false)] |
| | | 1387 | | public static ulong ToUInt64(int value) |
| | | 1388 | | { |
| | 0 | 1389 | | if (value < 0) ThrowUInt64OverflowException(); |
| | 0 | 1390 | | return (ulong)value; |
| | | 1391 | | } |
| | | 1392 | | |
| | | 1393 | | [CLSCompliant(false)] |
| | | 1394 | | public static ulong ToUInt64(uint value) |
| | | 1395 | | { |
| | 0 | 1396 | | return value; |
| | | 1397 | | } |
| | | 1398 | | |
| | | 1399 | | [CLSCompliant(false)] |
| | | 1400 | | public static ulong ToUInt64(long value) |
| | | 1401 | | { |
| | 0 | 1402 | | if (value < 0) ThrowUInt64OverflowException(); |
| | 0 | 1403 | | return (ulong)value; |
| | | 1404 | | } |
| | | 1405 | | |
| | | 1406 | | [CLSCompliant(false)] |
| | | 1407 | | public static ulong ToUInt64(ulong value) |
| | | 1408 | | { |
| | 0 | 1409 | | return value; |
| | | 1410 | | } |
| | | 1411 | | |
| | | 1412 | | [CLSCompliant(false)] |
| | | 1413 | | public static ulong ToUInt64(float value) |
| | | 1414 | | { |
| | 0 | 1415 | | return ToUInt64((double)value); |
| | | 1416 | | } |
| | | 1417 | | |
| | | 1418 | | [CLSCompliant(false)] |
| | | 1419 | | public static ulong ToUInt64(double value) |
| | | 1420 | | { |
| | 0 | 1421 | | return checked((ulong)Math.Round(value)); |
| | | 1422 | | } |
| | | 1423 | | |
| | | 1424 | | [CLSCompliant(false)] |
| | | 1425 | | public static ulong ToUInt64(decimal value) |
| | | 1426 | | { |
| | 0 | 1427 | | return decimal.ToUInt64(decimal.Round(value, 0)); |
| | | 1428 | | } |
| | | 1429 | | |
| | | 1430 | | [CLSCompliant(false)] |
| | | 1431 | | public static ulong ToUInt64(string? value) |
| | | 1432 | | { |
| | 0 | 1433 | | if (value == null) |
| | 0 | 1434 | | return 0; |
| | 0 | 1435 | | return ulong.Parse(value); |
| | | 1436 | | } |
| | | 1437 | | |
| | | 1438 | | [CLSCompliant(false)] |
| | | 1439 | | public static ulong ToUInt64(string? value, IFormatProvider? provider) |
| | | 1440 | | { |
| | 0 | 1441 | | if (value == null) |
| | 0 | 1442 | | return 0; |
| | 0 | 1443 | | return ulong.Parse(value, provider); |
| | | 1444 | | } |
| | | 1445 | | |
| | | 1446 | | [CLSCompliant(false)] |
| | | 1447 | | public static ulong ToUInt64(DateTime value) |
| | | 1448 | | { |
| | 0 | 1449 | | return ((IConvertible)value).ToUInt64(null); |
| | | 1450 | | } |
| | | 1451 | | |
| | | 1452 | | // Disallowed conversions to UInt64 |
| | | 1453 | | // public static ulong ToUInt64(TimeSpan value) |
| | | 1454 | | |
| | | 1455 | | // Conversions to Single |
| | | 1456 | | |
| | | 1457 | | public static float ToSingle(object? value) |
| | | 1458 | | { |
| | 0 | 1459 | | return value == null ? 0 : ((IConvertible)value).ToSingle(null); |
| | | 1460 | | } |
| | | 1461 | | |
| | | 1462 | | public static float ToSingle(object? value, IFormatProvider? provider) |
| | | 1463 | | { |
| | 0 | 1464 | | return value == null ? 0 : ((IConvertible)value).ToSingle(provider); |
| | | 1465 | | } |
| | | 1466 | | |
| | | 1467 | | [CLSCompliant(false)] |
| | | 1468 | | public static float ToSingle(sbyte value) |
| | | 1469 | | { |
| | 0 | 1470 | | return value; |
| | | 1471 | | } |
| | | 1472 | | |
| | | 1473 | | public static float ToSingle(byte value) |
| | | 1474 | | { |
| | 0 | 1475 | | return value; |
| | | 1476 | | } |
| | | 1477 | | |
| | | 1478 | | public static float ToSingle(char value) |
| | | 1479 | | { |
| | 0 | 1480 | | return ((IConvertible)value).ToSingle(null); |
| | | 1481 | | } |
| | | 1482 | | |
| | | 1483 | | public static float ToSingle(short value) |
| | | 1484 | | { |
| | 0 | 1485 | | return value; |
| | | 1486 | | } |
| | | 1487 | | |
| | | 1488 | | [CLSCompliant(false)] |
| | | 1489 | | public static float ToSingle(ushort value) |
| | | 1490 | | { |
| | 0 | 1491 | | return value; |
| | | 1492 | | } |
| | | 1493 | | |
| | | 1494 | | public static float ToSingle(int value) |
| | | 1495 | | { |
| | 0 | 1496 | | return value; |
| | | 1497 | | } |
| | | 1498 | | |
| | | 1499 | | [CLSCompliant(false)] |
| | | 1500 | | public static float ToSingle(uint value) |
| | | 1501 | | { |
| | 0 | 1502 | | return value; |
| | | 1503 | | } |
| | | 1504 | | |
| | | 1505 | | public static float ToSingle(long value) |
| | | 1506 | | { |
| | 0 | 1507 | | return value; |
| | | 1508 | | } |
| | | 1509 | | |
| | | 1510 | | [CLSCompliant(false)] |
| | | 1511 | | public static float ToSingle(ulong value) |
| | | 1512 | | { |
| | 0 | 1513 | | return value; |
| | | 1514 | | } |
| | | 1515 | | |
| | | 1516 | | public static float ToSingle(float value) |
| | | 1517 | | { |
| | 0 | 1518 | | return value; |
| | | 1519 | | } |
| | | 1520 | | |
| | | 1521 | | public static float ToSingle(double value) |
| | | 1522 | | { |
| | 0 | 1523 | | return (float)value; |
| | | 1524 | | } |
| | | 1525 | | |
| | | 1526 | | public static float ToSingle(decimal value) |
| | | 1527 | | { |
| | 0 | 1528 | | return (float)value; |
| | | 1529 | | } |
| | | 1530 | | |
| | | 1531 | | public static float ToSingle(string? value) |
| | | 1532 | | { |
| | 0 | 1533 | | if (value == null) |
| | 0 | 1534 | | return 0; |
| | 0 | 1535 | | return float.Parse(value); |
| | | 1536 | | } |
| | | 1537 | | |
| | | 1538 | | public static float ToSingle(string? value, IFormatProvider? provider) |
| | | 1539 | | { |
| | 0 | 1540 | | if (value == null) |
| | 0 | 1541 | | return 0; |
| | 0 | 1542 | | return float.Parse(value, provider); |
| | | 1543 | | } |
| | | 1544 | | |
| | | 1545 | | public static float ToSingle(bool value) |
| | | 1546 | | { |
| | 0 | 1547 | | return value ? bool.True : bool.False; |
| | | 1548 | | } |
| | | 1549 | | |
| | | 1550 | | public static float ToSingle(DateTime value) |
| | | 1551 | | { |
| | 0 | 1552 | | return ((IConvertible)value).ToSingle(null); |
| | | 1553 | | } |
| | | 1554 | | |
| | | 1555 | | // Disallowed conversions to Single |
| | | 1556 | | // public static float ToSingle(TimeSpan value) |
| | | 1557 | | |
| | | 1558 | | // Conversions to Double |
| | | 1559 | | |
| | | 1560 | | public static double ToDouble(object? value) |
| | | 1561 | | { |
| | 0 | 1562 | | return value == null ? 0 : ((IConvertible)value).ToDouble(null); |
| | | 1563 | | } |
| | | 1564 | | |
| | | 1565 | | public static double ToDouble(object? value, IFormatProvider? provider) |
| | | 1566 | | { |
| | 0 | 1567 | | return value == null ? 0 : ((IConvertible)value).ToDouble(provider); |
| | | 1568 | | } |
| | | 1569 | | |
| | | 1570 | | [CLSCompliant(false)] |
| | | 1571 | | public static double ToDouble(sbyte value) |
| | | 1572 | | { |
| | 0 | 1573 | | return value; |
| | | 1574 | | } |
| | | 1575 | | |
| | | 1576 | | public static double ToDouble(byte value) |
| | | 1577 | | { |
| | 0 | 1578 | | return value; |
| | | 1579 | | } |
| | | 1580 | | |
| | | 1581 | | public static double ToDouble(short value) |
| | | 1582 | | { |
| | 0 | 1583 | | return value; |
| | | 1584 | | } |
| | | 1585 | | |
| | | 1586 | | public static double ToDouble(char value) |
| | | 1587 | | { |
| | 0 | 1588 | | return ((IConvertible)value).ToDouble(null); |
| | | 1589 | | } |
| | | 1590 | | |
| | | 1591 | | [CLSCompliant(false)] |
| | | 1592 | | public static double ToDouble(ushort value) |
| | | 1593 | | { |
| | 0 | 1594 | | return value; |
| | | 1595 | | } |
| | | 1596 | | |
| | | 1597 | | public static double ToDouble(int value) |
| | | 1598 | | { |
| | 0 | 1599 | | return value; |
| | | 1600 | | } |
| | | 1601 | | |
| | | 1602 | | [CLSCompliant(false)] |
| | | 1603 | | public static double ToDouble(uint value) |
| | | 1604 | | { |
| | 0 | 1605 | | return value; |
| | | 1606 | | } |
| | | 1607 | | |
| | | 1608 | | public static double ToDouble(long value) |
| | | 1609 | | { |
| | 0 | 1610 | | return value; |
| | | 1611 | | } |
| | | 1612 | | |
| | | 1613 | | [CLSCompliant(false)] |
| | | 1614 | | public static double ToDouble(ulong value) |
| | | 1615 | | { |
| | 0 | 1616 | | return value; |
| | | 1617 | | } |
| | | 1618 | | |
| | | 1619 | | public static double ToDouble(float value) |
| | | 1620 | | { |
| | 0 | 1621 | | return value; |
| | | 1622 | | } |
| | | 1623 | | |
| | | 1624 | | public static double ToDouble(double value) |
| | | 1625 | | { |
| | 0 | 1626 | | return value; |
| | | 1627 | | } |
| | | 1628 | | |
| | | 1629 | | public static double ToDouble(decimal value) |
| | | 1630 | | { |
| | 0 | 1631 | | return (double)value; |
| | | 1632 | | } |
| | | 1633 | | |
| | | 1634 | | public static double ToDouble(string? value) |
| | | 1635 | | { |
| | 0 | 1636 | | if (value == null) |
| | 0 | 1637 | | return 0; |
| | 0 | 1638 | | return double.Parse(value); |
| | | 1639 | | } |
| | | 1640 | | |
| | | 1641 | | public static double ToDouble(string? value, IFormatProvider? provider) |
| | | 1642 | | { |
| | 0 | 1643 | | if (value == null) |
| | 0 | 1644 | | return 0; |
| | 0 | 1645 | | return double.Parse(value, provider); |
| | | 1646 | | } |
| | | 1647 | | |
| | | 1648 | | public static double ToDouble(bool value) |
| | | 1649 | | { |
| | 0 | 1650 | | return value ? bool.True : bool.False; |
| | | 1651 | | } |
| | | 1652 | | |
| | | 1653 | | public static double ToDouble(DateTime value) |
| | | 1654 | | { |
| | 0 | 1655 | | return ((IConvertible)value).ToDouble(null); |
| | | 1656 | | } |
| | | 1657 | | |
| | | 1658 | | // Disallowed conversions to Double |
| | | 1659 | | // public static double ToDouble(TimeSpan value) |
| | | 1660 | | |
| | | 1661 | | // Conversions to Decimal |
| | | 1662 | | |
| | | 1663 | | public static decimal ToDecimal(object? value) |
| | | 1664 | | { |
| | 0 | 1665 | | return value == null ? 0 : ((IConvertible)value).ToDecimal(null); |
| | | 1666 | | } |
| | | 1667 | | |
| | | 1668 | | public static decimal ToDecimal(object? value, IFormatProvider? provider) |
| | | 1669 | | { |
| | 0 | 1670 | | return value == null ? 0 : ((IConvertible)value).ToDecimal(provider); |
| | | 1671 | | } |
| | | 1672 | | |
| | | 1673 | | [CLSCompliant(false)] |
| | | 1674 | | public static decimal ToDecimal(sbyte value) |
| | | 1675 | | { |
| | 0 | 1676 | | return value; |
| | | 1677 | | } |
| | | 1678 | | |
| | | 1679 | | public static decimal ToDecimal(byte value) |
| | | 1680 | | { |
| | 0 | 1681 | | return value; |
| | | 1682 | | } |
| | | 1683 | | |
| | | 1684 | | public static decimal ToDecimal(char value) |
| | | 1685 | | { |
| | 0 | 1686 | | return ((IConvertible)value).ToDecimal(null); |
| | | 1687 | | } |
| | | 1688 | | |
| | | 1689 | | public static decimal ToDecimal(short value) |
| | | 1690 | | { |
| | 0 | 1691 | | return value; |
| | | 1692 | | } |
| | | 1693 | | |
| | | 1694 | | [CLSCompliant(false)] |
| | | 1695 | | public static decimal ToDecimal(ushort value) |
| | | 1696 | | { |
| | 0 | 1697 | | return value; |
| | | 1698 | | } |
| | | 1699 | | |
| | | 1700 | | public static decimal ToDecimal(int value) |
| | | 1701 | | { |
| | 0 | 1702 | | return value; |
| | | 1703 | | } |
| | | 1704 | | |
| | | 1705 | | [CLSCompliant(false)] |
| | | 1706 | | public static decimal ToDecimal(uint value) |
| | | 1707 | | { |
| | 0 | 1708 | | return value; |
| | | 1709 | | } |
| | | 1710 | | |
| | | 1711 | | public static decimal ToDecimal(long value) |
| | | 1712 | | { |
| | 0 | 1713 | | return value; |
| | | 1714 | | } |
| | | 1715 | | |
| | | 1716 | | [CLSCompliant(false)] |
| | | 1717 | | public static decimal ToDecimal(ulong value) |
| | | 1718 | | { |
| | 0 | 1719 | | return value; |
| | | 1720 | | } |
| | | 1721 | | |
| | | 1722 | | public static decimal ToDecimal(float value) |
| | | 1723 | | { |
| | 0 | 1724 | | return (decimal)value; |
| | | 1725 | | } |
| | | 1726 | | |
| | | 1727 | | public static decimal ToDecimal(double value) |
| | | 1728 | | { |
| | 0 | 1729 | | return (decimal)value; |
| | | 1730 | | } |
| | | 1731 | | |
| | | 1732 | | public static decimal ToDecimal(string? value) |
| | | 1733 | | { |
| | 0 | 1734 | | if (value == null) |
| | 0 | 1735 | | return 0m; |
| | 0 | 1736 | | return decimal.Parse(value); |
| | | 1737 | | } |
| | | 1738 | | |
| | | 1739 | | public static decimal ToDecimal(string? value, IFormatProvider? provider) |
| | | 1740 | | { |
| | 0 | 1741 | | if (value == null) |
| | 0 | 1742 | | return 0m; |
| | 0 | 1743 | | return decimal.Parse(value, provider); |
| | | 1744 | | } |
| | | 1745 | | |
| | | 1746 | | public static decimal ToDecimal(decimal value) |
| | | 1747 | | { |
| | 0 | 1748 | | return value; |
| | | 1749 | | } |
| | | 1750 | | |
| | | 1751 | | public static decimal ToDecimal(bool value) |
| | | 1752 | | { |
| | 0 | 1753 | | return value ? bool.True : bool.False; |
| | | 1754 | | } |
| | | 1755 | | |
| | | 1756 | | public static decimal ToDecimal(DateTime value) |
| | | 1757 | | { |
| | 0 | 1758 | | return ((IConvertible)value).ToDecimal(null); |
| | | 1759 | | } |
| | | 1760 | | |
| | | 1761 | | // Disallowed conversions to Decimal |
| | | 1762 | | // public static decimal ToDecimal(TimeSpan value) |
| | | 1763 | | |
| | | 1764 | | // Conversions to DateTime |
| | | 1765 | | |
| | | 1766 | | public static DateTime ToDateTime(DateTime value) |
| | | 1767 | | { |
| | 0 | 1768 | | return value; |
| | | 1769 | | } |
| | | 1770 | | |
| | | 1771 | | public static DateTime ToDateTime(object? value) |
| | | 1772 | | { |
| | 0 | 1773 | | return value == null ? DateTime.MinValue : ((IConvertible)value).ToDateTime(null); |
| | | 1774 | | } |
| | | 1775 | | |
| | | 1776 | | public static DateTime ToDateTime(object? value, IFormatProvider? provider) |
| | | 1777 | | { |
| | 0 | 1778 | | return value == null ? DateTime.MinValue : ((IConvertible)value).ToDateTime(provider); |
| | | 1779 | | } |
| | | 1780 | | |
| | | 1781 | | public static DateTime ToDateTime(string? value) |
| | | 1782 | | { |
| | 0 | 1783 | | if (value == null) |
| | 0 | 1784 | | return new DateTime(0); |
| | 0 | 1785 | | return DateTime.Parse(value); |
| | | 1786 | | } |
| | | 1787 | | |
| | | 1788 | | public static DateTime ToDateTime(string? value, IFormatProvider? provider) |
| | | 1789 | | { |
| | 0 | 1790 | | if (value == null) |
| | 0 | 1791 | | return new DateTime(0); |
| | 0 | 1792 | | return DateTime.Parse(value, provider); |
| | | 1793 | | } |
| | | 1794 | | |
| | | 1795 | | [CLSCompliant(false)] |
| | | 1796 | | public static DateTime ToDateTime(sbyte value) |
| | | 1797 | | { |
| | 0 | 1798 | | return ((IConvertible)value).ToDateTime(null); |
| | | 1799 | | } |
| | | 1800 | | |
| | | 1801 | | public static DateTime ToDateTime(byte value) |
| | | 1802 | | { |
| | 0 | 1803 | | return ((IConvertible)value).ToDateTime(null); |
| | | 1804 | | } |
| | | 1805 | | |
| | | 1806 | | public static DateTime ToDateTime(short value) |
| | | 1807 | | { |
| | 0 | 1808 | | return ((IConvertible)value).ToDateTime(null); |
| | | 1809 | | } |
| | | 1810 | | |
| | | 1811 | | [CLSCompliant(false)] |
| | | 1812 | | public static DateTime ToDateTime(ushort value) |
| | | 1813 | | { |
| | 0 | 1814 | | return ((IConvertible)value).ToDateTime(null); |
| | | 1815 | | } |
| | | 1816 | | |
| | | 1817 | | public static DateTime ToDateTime(int value) |
| | | 1818 | | { |
| | 0 | 1819 | | return ((IConvertible)value).ToDateTime(null); |
| | | 1820 | | } |
| | | 1821 | | |
| | | 1822 | | [CLSCompliant(false)] |
| | | 1823 | | public static DateTime ToDateTime(uint value) |
| | | 1824 | | { |
| | 0 | 1825 | | return ((IConvertible)value).ToDateTime(null); |
| | | 1826 | | } |
| | | 1827 | | |
| | | 1828 | | public static DateTime ToDateTime(long value) |
| | | 1829 | | { |
| | 0 | 1830 | | return ((IConvertible)value).ToDateTime(null); |
| | | 1831 | | } |
| | | 1832 | | |
| | | 1833 | | [CLSCompliant(false)] |
| | | 1834 | | public static DateTime ToDateTime(ulong value) |
| | | 1835 | | { |
| | 0 | 1836 | | return ((IConvertible)value).ToDateTime(null); |
| | | 1837 | | } |
| | | 1838 | | |
| | | 1839 | | public static DateTime ToDateTime(bool value) |
| | | 1840 | | { |
| | 0 | 1841 | | return ((IConvertible)value).ToDateTime(null); |
| | | 1842 | | } |
| | | 1843 | | |
| | | 1844 | | public static DateTime ToDateTime(char value) |
| | | 1845 | | { |
| | 0 | 1846 | | return ((IConvertible)value).ToDateTime(null); |
| | | 1847 | | } |
| | | 1848 | | |
| | | 1849 | | public static DateTime ToDateTime(float value) |
| | | 1850 | | { |
| | 0 | 1851 | | return ((IConvertible)value).ToDateTime(null); |
| | | 1852 | | } |
| | | 1853 | | |
| | | 1854 | | public static DateTime ToDateTime(double value) |
| | | 1855 | | { |
| | 0 | 1856 | | return ((IConvertible)value).ToDateTime(null); |
| | | 1857 | | } |
| | | 1858 | | |
| | | 1859 | | public static DateTime ToDateTime(decimal value) |
| | | 1860 | | { |
| | 0 | 1861 | | return ((IConvertible)value).ToDateTime(null); |
| | | 1862 | | } |
| | | 1863 | | |
| | | 1864 | | // Disallowed conversions to DateTime |
| | | 1865 | | // public static DateTime ToDateTime(TimeSpan value) |
| | | 1866 | | |
| | | 1867 | | // Conversions to String |
| | | 1868 | | |
| | | 1869 | | public static string? ToString(object? value) |
| | | 1870 | | { |
| | 0 | 1871 | | return ToString(value, null); |
| | | 1872 | | } |
| | | 1873 | | |
| | | 1874 | | public static string? ToString(object? value, IFormatProvider? provider) |
| | | 1875 | | { |
| | 0 | 1876 | | if (value is IConvertible ic) |
| | 0 | 1877 | | return ic.ToString(provider); |
| | 0 | 1878 | | if (value is IFormattable formattable) |
| | 0 | 1879 | | return formattable.ToString(null, provider); |
| | 0 | 1880 | | return value == null ? string.Empty : value.ToString(); |
| | | 1881 | | } |
| | | 1882 | | |
| | | 1883 | | public static string ToString(bool value) |
| | | 1884 | | { |
| | 0 | 1885 | | return value.ToString(); |
| | | 1886 | | } |
| | | 1887 | | |
| | | 1888 | | public static string ToString(bool value, IFormatProvider? provider) |
| | | 1889 | | { |
| | 0 | 1890 | | return value.ToString(); |
| | | 1891 | | } |
| | | 1892 | | |
| | | 1893 | | public static string ToString(char value) |
| | | 1894 | | { |
| | 0 | 1895 | | return char.ToString(value); |
| | | 1896 | | } |
| | | 1897 | | |
| | | 1898 | | public static string ToString(char value, IFormatProvider? provider) |
| | | 1899 | | { |
| | 0 | 1900 | | return value.ToString(); |
| | | 1901 | | } |
| | | 1902 | | |
| | | 1903 | | [CLSCompliant(false)] |
| | | 1904 | | public static string ToString(sbyte value) |
| | | 1905 | | { |
| | 0 | 1906 | | return value.ToString(); |
| | | 1907 | | } |
| | | 1908 | | |
| | | 1909 | | [CLSCompliant(false)] |
| | | 1910 | | public static string ToString(sbyte value, IFormatProvider? provider) |
| | | 1911 | | { |
| | 0 | 1912 | | return value.ToString(provider); |
| | | 1913 | | } |
| | | 1914 | | |
| | | 1915 | | public static string ToString(byte value) |
| | | 1916 | | { |
| | 0 | 1917 | | return value.ToString(); |
| | | 1918 | | } |
| | | 1919 | | |
| | | 1920 | | public static string ToString(byte value, IFormatProvider? provider) |
| | | 1921 | | { |
| | 0 | 1922 | | return value.ToString(provider); |
| | | 1923 | | } |
| | | 1924 | | |
| | | 1925 | | public static string ToString(short value) |
| | | 1926 | | { |
| | 0 | 1927 | | return value.ToString(); |
| | | 1928 | | } |
| | | 1929 | | |
| | | 1930 | | public static string ToString(short value, IFormatProvider? provider) |
| | | 1931 | | { |
| | 0 | 1932 | | return value.ToString(provider); |
| | | 1933 | | } |
| | | 1934 | | |
| | | 1935 | | [CLSCompliant(false)] |
| | | 1936 | | public static string ToString(ushort value) |
| | | 1937 | | { |
| | 0 | 1938 | | return value.ToString(); |
| | | 1939 | | } |
| | | 1940 | | |
| | | 1941 | | [CLSCompliant(false)] |
| | | 1942 | | public static string ToString(ushort value, IFormatProvider? provider) |
| | | 1943 | | { |
| | 0 | 1944 | | return value.ToString(provider); |
| | | 1945 | | } |
| | | 1946 | | |
| | | 1947 | | public static string ToString(int value) |
| | | 1948 | | { |
| | 0 | 1949 | | return value.ToString(); |
| | | 1950 | | } |
| | | 1951 | | |
| | | 1952 | | public static string ToString(int value, IFormatProvider? provider) |
| | | 1953 | | { |
| | 0 | 1954 | | return value.ToString(provider); |
| | | 1955 | | } |
| | | 1956 | | |
| | | 1957 | | [CLSCompliant(false)] |
| | | 1958 | | public static string ToString(uint value) |
| | | 1959 | | { |
| | 0 | 1960 | | return value.ToString(); |
| | | 1961 | | } |
| | | 1962 | | |
| | | 1963 | | [CLSCompliant(false)] |
| | | 1964 | | public static string ToString(uint value, IFormatProvider? provider) |
| | | 1965 | | { |
| | 0 | 1966 | | return value.ToString(provider); |
| | | 1967 | | } |
| | | 1968 | | |
| | | 1969 | | public static string ToString(long value) |
| | | 1970 | | { |
| | 0 | 1971 | | return value.ToString(); |
| | | 1972 | | } |
| | | 1973 | | |
| | | 1974 | | public static string ToString(long value, IFormatProvider? provider) |
| | | 1975 | | { |
| | 0 | 1976 | | return value.ToString(provider); |
| | | 1977 | | } |
| | | 1978 | | |
| | | 1979 | | [CLSCompliant(false)] |
| | | 1980 | | public static string ToString(ulong value) |
| | | 1981 | | { |
| | 0 | 1982 | | return value.ToString(); |
| | | 1983 | | } |
| | | 1984 | | |
| | | 1985 | | [CLSCompliant(false)] |
| | | 1986 | | public static string ToString(ulong value, IFormatProvider? provider) |
| | | 1987 | | { |
| | 0 | 1988 | | return value.ToString(provider); |
| | | 1989 | | } |
| | | 1990 | | |
| | | 1991 | | public static string ToString(float value) |
| | | 1992 | | { |
| | 0 | 1993 | | return value.ToString(); |
| | | 1994 | | } |
| | | 1995 | | |
| | | 1996 | | public static string ToString(float value, IFormatProvider? provider) |
| | | 1997 | | { |
| | 0 | 1998 | | return value.ToString(provider); |
| | | 1999 | | } |
| | | 2000 | | |
| | | 2001 | | public static string ToString(double value) |
| | | 2002 | | { |
| | 0 | 2003 | | return value.ToString(); |
| | | 2004 | | } |
| | | 2005 | | |
| | | 2006 | | public static string ToString(double value, IFormatProvider? provider) |
| | | 2007 | | { |
| | 0 | 2008 | | return value.ToString(provider); |
| | | 2009 | | } |
| | | 2010 | | |
| | | 2011 | | public static string ToString(decimal value) |
| | | 2012 | | { |
| | 0 | 2013 | | return value.ToString(); |
| | | 2014 | | } |
| | | 2015 | | |
| | | 2016 | | public static string ToString(decimal value, IFormatProvider? provider) |
| | | 2017 | | { |
| | 0 | 2018 | | return value.ToString(provider); |
| | | 2019 | | } |
| | | 2020 | | |
| | | 2021 | | public static string ToString(DateTime value) |
| | | 2022 | | { |
| | 0 | 2023 | | return value.ToString(); |
| | | 2024 | | } |
| | | 2025 | | |
| | | 2026 | | public static string ToString(DateTime value, IFormatProvider? provider) |
| | | 2027 | | { |
| | 0 | 2028 | | return value.ToString(provider); |
| | | 2029 | | } |
| | | 2030 | | |
| | | 2031 | | [return: NotNullIfNotNull(nameof(value))] |
| | | 2032 | | public static string? ToString(string? value) |
| | | 2033 | | { |
| | 0 | 2034 | | return value; |
| | | 2035 | | } |
| | | 2036 | | |
| | | 2037 | | [return: NotNullIfNotNull(nameof(value))] |
| | | 2038 | | public static string? ToString(string? value, IFormatProvider? provider) |
| | | 2039 | | { |
| | 0 | 2040 | | return value; |
| | | 2041 | | } |
| | | 2042 | | |
| | | 2043 | | // |
| | | 2044 | | // Conversions which understand Base XXX numbers. |
| | | 2045 | | // |
| | | 2046 | | // Parses value in base base. base can only |
| | | 2047 | | // be 2, 8, 10, or 16. If base is 16, the number may be preceded |
| | | 2048 | | // by 0x or 0X; any other leading or trailing characters cause an error. |
| | | 2049 | | // |
| | | 2050 | | public static byte ToByte(string? value, int fromBase) |
| | | 2051 | | { |
| | 0 | 2052 | | if (fromBase != 2 && fromBase != 8 && fromBase != 10 && fromBase != 16) |
| | | 2053 | | { |
| | 0 | 2054 | | ThrowInvalidBase(); |
| | | 2055 | | } |
| | | 2056 | | |
| | 0 | 2057 | | if (value == null) |
| | | 2058 | | { |
| | 0 | 2059 | | return 0; |
| | | 2060 | | } |
| | | 2061 | | |
| | 0 | 2062 | | int r = ParseNumbers.StringToInt(value.AsSpan(), fromBase, ParseNumbers.IsTight | ParseNumbers.TreatAsUnsign |
| | 0 | 2063 | | if ((uint)r > byte.MaxValue) |
| | 0 | 2064 | | ThrowByteOverflowException(); |
| | 0 | 2065 | | return (byte)r; |
| | | 2066 | | } |
| | | 2067 | | |
| | | 2068 | | // Parses value in base fromBase. fromBase can only |
| | | 2069 | | // be 2, 8, 10, or 16. If fromBase is 16, the number may be preceded |
| | | 2070 | | // by 0x or 0X; any other leading or trailing characters cause an error. |
| | | 2071 | | // |
| | | 2072 | | [CLSCompliant(false)] |
| | | 2073 | | public static sbyte ToSByte(string? value, int fromBase) |
| | | 2074 | | { |
| | 0 | 2075 | | if (fromBase != 2 && fromBase != 8 && fromBase != 10 && fromBase != 16) |
| | | 2076 | | { |
| | 0 | 2077 | | ThrowInvalidBase(); |
| | | 2078 | | } |
| | | 2079 | | |
| | 0 | 2080 | | if (value == null) |
| | | 2081 | | { |
| | 0 | 2082 | | return 0; |
| | | 2083 | | } |
| | | 2084 | | |
| | 0 | 2085 | | int r = ParseNumbers.StringToInt(value.AsSpan(), fromBase, ParseNumbers.IsTight | ParseNumbers.TreatAsI1); |
| | 0 | 2086 | | if (fromBase != 10 && r <= byte.MaxValue) |
| | 0 | 2087 | | return (sbyte)r; |
| | | 2088 | | |
| | 0 | 2089 | | if (r < sbyte.MinValue || r > sbyte.MaxValue) |
| | 0 | 2090 | | ThrowSByteOverflowException(); |
| | 0 | 2091 | | return (sbyte)r; |
| | | 2092 | | } |
| | | 2093 | | |
| | | 2094 | | // Parses value in base fromBase. fromBase can only |
| | | 2095 | | // be 2, 8, 10, or 16. If fromBase is 16, the number may be preceded |
| | | 2096 | | // by 0x or 0X; any other leading or trailing characters cause an error. |
| | | 2097 | | // |
| | | 2098 | | public static short ToInt16(string? value, int fromBase) |
| | | 2099 | | { |
| | 0 | 2100 | | if (fromBase != 2 && fromBase != 8 && fromBase != 10 && fromBase != 16) |
| | | 2101 | | { |
| | 0 | 2102 | | ThrowInvalidBase(); |
| | | 2103 | | } |
| | | 2104 | | |
| | 0 | 2105 | | if (value == null) |
| | | 2106 | | { |
| | 0 | 2107 | | return 0; |
| | | 2108 | | } |
| | | 2109 | | |
| | 0 | 2110 | | int r = ParseNumbers.StringToInt(value.AsSpan(), fromBase, ParseNumbers.IsTight | ParseNumbers.TreatAsI2); |
| | 0 | 2111 | | if (fromBase != 10 && r <= ushort.MaxValue) |
| | 0 | 2112 | | return (short)r; |
| | | 2113 | | |
| | 0 | 2114 | | if (r < short.MinValue || r > short.MaxValue) |
| | 0 | 2115 | | ThrowInt16OverflowException(); |
| | 0 | 2116 | | return (short)r; |
| | | 2117 | | } |
| | | 2118 | | |
| | | 2119 | | // Parses value in base fromBase. fromBase can only |
| | | 2120 | | // be 2, 8, 10, or 16. If fromBase is 16, the number may be preceded |
| | | 2121 | | // by 0x or 0X; any other leading or trailing characters cause an error. |
| | | 2122 | | // |
| | | 2123 | | [CLSCompliant(false)] |
| | | 2124 | | public static ushort ToUInt16(string? value, int fromBase) |
| | | 2125 | | { |
| | 0 | 2126 | | if (fromBase != 2 && fromBase != 8 && fromBase != 10 && fromBase != 16) |
| | | 2127 | | { |
| | 0 | 2128 | | ThrowInvalidBase(); |
| | | 2129 | | } |
| | | 2130 | | |
| | 0 | 2131 | | if (value == null) |
| | | 2132 | | { |
| | 0 | 2133 | | return 0; |
| | | 2134 | | } |
| | | 2135 | | |
| | 0 | 2136 | | int r = ParseNumbers.StringToInt(value.AsSpan(), fromBase, ParseNumbers.IsTight | ParseNumbers.TreatAsUnsign |
| | 0 | 2137 | | if ((uint)r > ushort.MaxValue) |
| | 0 | 2138 | | ThrowUInt16OverflowException(); |
| | 0 | 2139 | | return (ushort)r; |
| | | 2140 | | } |
| | | 2141 | | |
| | | 2142 | | // Parses value in base fromBase. fromBase can only |
| | | 2143 | | // be 2, 8, 10, or 16. If fromBase is 16, the number may be preceded |
| | | 2144 | | // by 0x or 0X; any other leading or trailing characters cause an error. |
| | | 2145 | | // |
| | | 2146 | | public static int ToInt32(string? value, int fromBase) |
| | | 2147 | | { |
| | 0 | 2148 | | if (fromBase != 2 && fromBase != 8 && fromBase != 10 && fromBase != 16) |
| | | 2149 | | { |
| | 0 | 2150 | | ThrowInvalidBase(); |
| | | 2151 | | } |
| | 0 | 2152 | | return value != null ? |
| | 0 | 2153 | | ParseNumbers.StringToInt(value.AsSpan(), fromBase, ParseNumbers.IsTight) : |
| | 0 | 2154 | | 0; |
| | | 2155 | | } |
| | | 2156 | | |
| | | 2157 | | // Parses value in base fromBase. fromBase can only |
| | | 2158 | | // be 2, 8, 10, or 16. If fromBase is 16, the number may be preceded |
| | | 2159 | | // by 0x or 0X; any other leading or trailing characters cause an error. |
| | | 2160 | | // |
| | | 2161 | | [CLSCompliant(false)] |
| | | 2162 | | public static uint ToUInt32(string? value, int fromBase) |
| | | 2163 | | { |
| | 0 | 2164 | | if (fromBase != 2 && fromBase != 8 && fromBase != 10 && fromBase != 16) |
| | | 2165 | | { |
| | 0 | 2166 | | ThrowInvalidBase(); |
| | | 2167 | | } |
| | 0 | 2168 | | return value != null ? |
| | 0 | 2169 | | (uint)ParseNumbers.StringToInt(value.AsSpan(), fromBase, ParseNumbers.TreatAsUnsigned | ParseNumbers.IsT |
| | 0 | 2170 | | 0; |
| | | 2171 | | } |
| | | 2172 | | |
| | | 2173 | | // Parses value in base fromBase. fromBase can only |
| | | 2174 | | // be 2, 8, 10, or 16. If fromBase is 16, the number may be preceded |
| | | 2175 | | // by 0x or 0X; any other leading or trailing characters cause an error. |
| | | 2176 | | // |
| | | 2177 | | public static long ToInt64(string? value, int fromBase) |
| | | 2178 | | { |
| | 0 | 2179 | | if (fromBase != 2 && fromBase != 8 && fromBase != 10 && fromBase != 16) |
| | | 2180 | | { |
| | 0 | 2181 | | ThrowInvalidBase(); |
| | | 2182 | | } |
| | 0 | 2183 | | return value != null ? |
| | 0 | 2184 | | ParseNumbers.StringToLong(value.AsSpan(), fromBase, ParseNumbers.IsTight) : |
| | 0 | 2185 | | 0; |
| | | 2186 | | } |
| | | 2187 | | |
| | | 2188 | | // Parses value in base fromBase. fromBase can only |
| | | 2189 | | // be 2, 8, 10, or 16. If fromBase is 16, the number may be preceded |
| | | 2190 | | // by 0x or 0X; any other leading or trailing characters cause an error. |
| | | 2191 | | // |
| | | 2192 | | [CLSCompliant(false)] |
| | | 2193 | | public static ulong ToUInt64(string? value, int fromBase) |
| | | 2194 | | { |
| | 0 | 2195 | | if (fromBase != 2 && fromBase != 8 && fromBase != 10 && fromBase != 16) |
| | | 2196 | | { |
| | 0 | 2197 | | ThrowInvalidBase(); |
| | | 2198 | | } |
| | 0 | 2199 | | return value != null ? |
| | 0 | 2200 | | (ulong)ParseNumbers.StringToLong(value.AsSpan(), fromBase, ParseNumbers.TreatAsUnsigned | ParseNumbers.I |
| | 0 | 2201 | | 0; |
| | | 2202 | | } |
| | | 2203 | | |
| | | 2204 | | // Convert the byte value to a string in base toBase |
| | | 2205 | | public static string ToString(byte value, int toBase) => |
| | 0 | 2206 | | ToString((int)value, toBase); |
| | | 2207 | | |
| | | 2208 | | // Convert the Int16 value to a string in base toBase |
| | | 2209 | | public static string ToString(short value, int toBase) |
| | | 2210 | | { |
| | 0 | 2211 | | string format = "d"; |
| | | 2212 | | |
| | | 2213 | | switch (toBase) |
| | | 2214 | | { |
| | | 2215 | | case 2: |
| | 0 | 2216 | | format = "b"; |
| | 0 | 2217 | | break; |
| | | 2218 | | |
| | | 2219 | | case 8: |
| | 0 | 2220 | | return ToOctalString((ushort)value); |
| | | 2221 | | |
| | | 2222 | | case 10: |
| | | 2223 | | break; |
| | | 2224 | | |
| | | 2225 | | case 16: |
| | 0 | 2226 | | format = "x"; |
| | 0 | 2227 | | break; |
| | | 2228 | | |
| | | 2229 | | default: |
| | 0 | 2230 | | ThrowInvalidBase(); |
| | | 2231 | | break; |
| | | 2232 | | }; |
| | | 2233 | | |
| | 0 | 2234 | | return value.ToString(format, CultureInfo.InvariantCulture); |
| | | 2235 | | } |
| | | 2236 | | |
| | | 2237 | | // Convert the Int32 value to a string in base toBase |
| | | 2238 | | public static string ToString(int value, int toBase) |
| | | 2239 | | { |
| | 0 | 2240 | | string format = "d"; |
| | | 2241 | | |
| | | 2242 | | switch (toBase) |
| | | 2243 | | { |
| | | 2244 | | case 2: |
| | 0 | 2245 | | format = "b"; |
| | 0 | 2246 | | break; |
| | | 2247 | | |
| | | 2248 | | case 8: |
| | 0 | 2249 | | return ToOctalString((uint)value); |
| | | 2250 | | |
| | | 2251 | | case 10: |
| | | 2252 | | break; |
| | | 2253 | | |
| | | 2254 | | case 16: |
| | 0 | 2255 | | format = "x"; |
| | 0 | 2256 | | break; |
| | | 2257 | | |
| | | 2258 | | default: |
| | 0 | 2259 | | ThrowInvalidBase(); |
| | | 2260 | | break; |
| | | 2261 | | }; |
| | | 2262 | | |
| | 0 | 2263 | | return value.ToString(format, CultureInfo.InvariantCulture); |
| | | 2264 | | } |
| | | 2265 | | |
| | | 2266 | | // Convert the Int64 value to a string in base toBase |
| | | 2267 | | public static string ToString(long value, int toBase) |
| | | 2268 | | { |
| | 0 | 2269 | | string format = "d"; |
| | | 2270 | | |
| | | 2271 | | switch (toBase) |
| | | 2272 | | { |
| | | 2273 | | case 2: |
| | 0 | 2274 | | format = "b"; |
| | 0 | 2275 | | break; |
| | | 2276 | | |
| | | 2277 | | case 8: |
| | 0 | 2278 | | return ToOctalString((ulong)value); |
| | | 2279 | | |
| | | 2280 | | case 10: |
| | | 2281 | | break; |
| | | 2282 | | |
| | | 2283 | | case 16: |
| | 0 | 2284 | | format = "x"; |
| | 0 | 2285 | | break; |
| | | 2286 | | |
| | | 2287 | | default: |
| | 0 | 2288 | | ThrowInvalidBase(); |
| | | 2289 | | break; |
| | | 2290 | | }; |
| | | 2291 | | |
| | 0 | 2292 | | return value.ToString(format, CultureInfo.InvariantCulture); |
| | | 2293 | | } |
| | | 2294 | | |
| | 0 | 2295 | | private static void ThrowInvalidBase() => throw new ArgumentException(SR.Arg_InvalidBase); |
| | | 2296 | | |
| | | 2297 | | private static string ToOctalString(ulong value) |
| | | 2298 | | { |
| | 0 | 2299 | | Span<char> chars = stackalloc char[22]; // max length of a ulong in octal |
| | | 2300 | | |
| | 0 | 2301 | | int i = chars.Length; |
| | | 2302 | | do |
| | | 2303 | | { |
| | 0 | 2304 | | chars[--i] = (char)('0' + (value & 7)); |
| | 0 | 2305 | | value >>= 3; |
| | | 2306 | | } |
| | 0 | 2307 | | while (value != 0); |
| | | 2308 | | |
| | 0 | 2309 | | return chars.Slice(i).ToString(); |
| | | 2310 | | } |
| | | 2311 | | |
| | | 2312 | | public static string ToBase64String(byte[] inArray) |
| | | 2313 | | { |
| | 0 | 2314 | | ArgumentNullException.ThrowIfNull(inArray); |
| | | 2315 | | |
| | 0 | 2316 | | return Base64.EncodeToString(inArray); |
| | | 2317 | | } |
| | | 2318 | | |
| | | 2319 | | public static string ToBase64String(byte[] inArray, Base64FormattingOptions options) |
| | | 2320 | | { |
| | 0 | 2321 | | ArgumentNullException.ThrowIfNull(inArray); |
| | | 2322 | | |
| | 0 | 2323 | | return ToBase64String(new ReadOnlySpan<byte>(inArray), options); |
| | | 2324 | | } |
| | | 2325 | | |
| | | 2326 | | public static string ToBase64String(byte[] inArray, int offset, int length) |
| | | 2327 | | { |
| | 0 | 2328 | | return ToBase64String(inArray, offset, length, Base64FormattingOptions.None); |
| | | 2329 | | } |
| | | 2330 | | |
| | | 2331 | | public static string ToBase64String(byte[] inArray, int offset, int length, Base64FormattingOptions options) |
| | | 2332 | | { |
| | 0 | 2333 | | ArgumentNullException.ThrowIfNull(inArray); |
| | | 2334 | | |
| | 0 | 2335 | | ArgumentOutOfRangeException.ThrowIfNegative(length); |
| | 0 | 2336 | | ArgumentOutOfRangeException.ThrowIfNegative(offset); |
| | 0 | 2337 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(offset, inArray.Length - length); |
| | | 2338 | | |
| | 0 | 2339 | | return ToBase64String(new ReadOnlySpan<byte>(inArray, offset, length), options); |
| | | 2340 | | } |
| | | 2341 | | |
| | | 2342 | | public static string ToBase64String(ReadOnlySpan<byte> bytes, Base64FormattingOptions options = Base64Formatting |
| | | 2343 | | { |
| | 23836 | 2344 | | if ((uint)options > (uint)Base64FormattingOptions.InsertLineBreaks) |
| | | 2345 | | { |
| | 0 | 2346 | | throw new ArgumentException(SR.Format(SR.Arg_EnumIllegalVal, (int)options), nameof(options)); |
| | | 2347 | | } |
| | | 2348 | | |
| | 23836 | 2349 | | if (bytes.Length == 0) |
| | | 2350 | | { |
| | 0 | 2351 | | return string.Empty; |
| | | 2352 | | } |
| | | 2353 | | |
| | 23836 | 2354 | | bool insertLineBreaks = (options == Base64FormattingOptions.InsertLineBreaks); |
| | | 2355 | | |
| | 23836 | 2356 | | if (!insertLineBreaks) |
| | | 2357 | | { |
| | 11918 | 2358 | | return Base64.EncodeToString(bytes); |
| | | 2359 | | } |
| | | 2360 | | |
| | 11918 | 2361 | | int outputLength = ToBase64_CalculateAndValidateOutputLength(bytes.Length, insertLineBreaks: true); |
| | | 2362 | | |
| | 11918 | 2363 | | return string.Create(outputLength, bytes, static (buffer, bytes) => |
| | 11918 | 2364 | | { |
| | 11918 | 2365 | | int charsWritten = ConvertToBase64WithLineBreaks(buffer, bytes); |
| | 11918 | 2366 | | Debug.Assert(buffer.Length == charsWritten, $"Expected {buffer.Length} == {charsWritten}"); |
| | 23836 | 2367 | | }); |
| | | 2368 | | } |
| | | 2369 | | |
| | | 2370 | | public static int ToBase64CharArray(byte[] inArray, int offsetIn, int length, char[] outArray, int offsetOut) |
| | | 2371 | | { |
| | 0 | 2372 | | return ToBase64CharArray(inArray, offsetIn, length, outArray, offsetOut, Base64FormattingOptions.None); |
| | | 2373 | | } |
| | | 2374 | | |
| | | 2375 | | public static int ToBase64CharArray(byte[] inArray, int offsetIn, int length, char[] outArray, int offsetOut, Ba |
| | | 2376 | | { |
| | 23836 | 2377 | | ArgumentNullException.ThrowIfNull(inArray); |
| | 23836 | 2378 | | ArgumentNullException.ThrowIfNull(outArray); |
| | | 2379 | | |
| | 23836 | 2380 | | ArgumentOutOfRangeException.ThrowIfNegative(length); |
| | 23836 | 2381 | | ArgumentOutOfRangeException.ThrowIfNegative(offsetIn); |
| | 23836 | 2382 | | ArgumentOutOfRangeException.ThrowIfNegative(offsetOut); |
| | 23836 | 2383 | | if (options < Base64FormattingOptions.None || options > Base64FormattingOptions.InsertLineBreaks) |
| | 0 | 2384 | | throw new ArgumentException(SR.Format(SR.Arg_EnumIllegalVal, (int)options), nameof(options)); |
| | | 2385 | | |
| | 23836 | 2386 | | int inArrayLength = inArray.Length; |
| | | 2387 | | |
| | 23836 | 2388 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(offsetIn, inArrayLength - length); |
| | | 2389 | | |
| | 23836 | 2390 | | if (length == 0) |
| | 0 | 2391 | | return 0; |
| | | 2392 | | |
| | | 2393 | | // This is the maximally required length that must be available in the char array |
| | 23836 | 2394 | | int outArrayLength = outArray.Length; |
| | | 2395 | | |
| | | 2396 | | // Length of the char buffer required |
| | 23836 | 2397 | | bool insertLineBreaks = options == Base64FormattingOptions.InsertLineBreaks; |
| | 23836 | 2398 | | int charLengthRequired = ToBase64_CalculateAndValidateOutputLength(length, insertLineBreaks); |
| | | 2399 | | |
| | 23836 | 2400 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(offsetOut, outArrayLength - charLengthRequired); |
| | | 2401 | | |
| | 23836 | 2402 | | if (!insertLineBreaks) |
| | | 2403 | | { |
| | 11918 | 2404 | | int charsWritten = Base64.EncodeToChars(new ReadOnlySpan<byte>(inArray, offsetIn, length), outArray.AsSp |
| | 11918 | 2405 | | Debug.Assert(charsWritten == charLengthRequired); |
| | | 2406 | | } |
| | | 2407 | | else |
| | | 2408 | | { |
| | 11918 | 2409 | | int converted = ConvertToBase64WithLineBreaks(outArray.AsSpan(offsetOut), new ReadOnlySpan<byte>(inArray |
| | 11918 | 2410 | | Debug.Assert(converted == charLengthRequired); |
| | | 2411 | | } |
| | | 2412 | | |
| | 23836 | 2413 | | return charLengthRequired; |
| | | 2414 | | } |
| | | 2415 | | |
| | | 2416 | | public static bool TryToBase64Chars(ReadOnlySpan<byte> bytes, Span<char> chars, out int charsWritten, Base64Form |
| | | 2417 | | { |
| | 0 | 2418 | | if ((uint)options > (uint)Base64FormattingOptions.InsertLineBreaks) |
| | | 2419 | | { |
| | 0 | 2420 | | throw new ArgumentException(SR.Format(SR.Arg_EnumIllegalVal, (int)options), nameof(options)); |
| | | 2421 | | } |
| | | 2422 | | |
| | 0 | 2423 | | if (bytes.Length == 0) |
| | | 2424 | | { |
| | 0 | 2425 | | charsWritten = 0; |
| | 0 | 2426 | | return true; |
| | | 2427 | | } |
| | | 2428 | | |
| | 0 | 2429 | | bool insertLineBreaks = options == Base64FormattingOptions.InsertLineBreaks; |
| | | 2430 | | |
| | 0 | 2431 | | int charLengthRequired = ToBase64_CalculateAndValidateOutputLength(bytes.Length, insertLineBreaks); |
| | 0 | 2432 | | if (charLengthRequired > chars.Length) |
| | | 2433 | | { |
| | 0 | 2434 | | charsWritten = 0; |
| | 0 | 2435 | | return false; |
| | | 2436 | | } |
| | | 2437 | | |
| | 0 | 2438 | | if (!insertLineBreaks) |
| | | 2439 | | { |
| | 0 | 2440 | | int written = Base64.EncodeToChars(bytes, chars); |
| | 0 | 2441 | | Debug.Assert(written == charLengthRequired); |
| | | 2442 | | } |
| | | 2443 | | else |
| | | 2444 | | { |
| | 0 | 2445 | | int converted = ConvertToBase64WithLineBreaks(chars, bytes); |
| | 0 | 2446 | | Debug.Assert(converted == charLengthRequired); |
| | | 2447 | | } |
| | | 2448 | | |
| | 0 | 2449 | | charsWritten = charLengthRequired; |
| | 0 | 2450 | | return true; |
| | | 2451 | | } |
| | | 2452 | | |
| | | 2453 | | private static int ConvertToBase64WithLineBreaks(Span<char> destination, ReadOnlySpan<byte> source) |
| | | 2454 | | { |
| | 23836 | 2455 | | Debug.Assert(destination.Length >= ToBase64_CalculateAndValidateOutputLength(source.Length, insertLineBreaks |
| | | 2456 | | |
| | 23836 | 2457 | | int writeOffset = 0; |
| | | 2458 | | |
| | 214540 | 2459 | | while (true) |
| | | 2460 | | { |
| | 238376 | 2461 | | int chunkSize = Math.Min(source.Length, Base64LineBreakPosition / 4 * 3); // 76 base64 chars == 57 bytes |
| | | 2462 | | |
| | 238376 | 2463 | | OperationStatus status = Base64.EncodeToChars(source.Slice(0, chunkSize), destination.Slice(writeOffset) |
| | 238376 | 2464 | | Debug.Assert(status == OperationStatus.Done && bytesConsumed == chunkSize); |
| | | 2465 | | |
| | 238376 | 2466 | | source = source.Slice(chunkSize); |
| | 238376 | 2467 | | writeOffset += charsWritten; |
| | | 2468 | | |
| | 238376 | 2469 | | if (source.IsEmpty) |
| | | 2470 | | { |
| | | 2471 | | break; |
| | | 2472 | | } |
| | | 2473 | | |
| | 214540 | 2474 | | destination[writeOffset++] = '\r'; |
| | 214540 | 2475 | | destination[writeOffset++] = '\n'; |
| | | 2476 | | } |
| | | 2477 | | |
| | 23836 | 2478 | | return writeOffset; |
| | | 2479 | | } |
| | | 2480 | | |
| | | 2481 | | private static int ToBase64_CalculateAndValidateOutputLength(int inputLength, bool insertLineBreaks) |
| | | 2482 | | { |
| | | 2483 | | // the base length - we want integer division here, at most 4 more chars for the remainder |
| | 59590 | 2484 | | uint outlen = ((uint)inputLength + 2) / 3 * 4; |
| | | 2485 | | |
| | 59590 | 2486 | | if (outlen == 0) |
| | 0 | 2487 | | return 0; |
| | | 2488 | | |
| | 59590 | 2489 | | if (insertLineBreaks) |
| | | 2490 | | { |
| | 47672 | 2491 | | (uint newLines, uint remainder) = Math.DivRem(outlen, Base64LineBreakPosition); |
| | 47672 | 2492 | | if (remainder == 0) |
| | | 2493 | | { |
| | 1832 | 2494 | | --newLines; |
| | | 2495 | | } |
| | 47672 | 2496 | | outlen += newLines * 2; // the number of line break chars we'll add, "\r\n" |
| | | 2497 | | } |
| | | 2498 | | |
| | | 2499 | | // If we overflow an int then we cannot allocate enough |
| | | 2500 | | // memory to output the value so throw |
| | 59590 | 2501 | | if (outlen > int.MaxValue) |
| | 0 | 2502 | | throw new OutOfMemoryException(); |
| | | 2503 | | |
| | 59590 | 2504 | | return (int)outlen; |
| | | 2505 | | } |
| | | 2506 | | |
| | | 2507 | | /// <summary> |
| | | 2508 | | /// Converts the specified string, which encodes binary data as Base64 digits, to the equivalent byte array. |
| | | 2509 | | /// </summary> |
| | | 2510 | | /// <param name="s">The string to convert</param> |
| | | 2511 | | /// <returns>The array of bytes represented by the specified Base64 string.</returns> |
| | | 2512 | | public static byte[] FromBase64String(string s) |
| | | 2513 | | { |
| | | 2514 | | // "s" is an unfortunate parameter name, but we need to keep it for backward compat. |
| | | 2515 | | |
| | 23836 | 2516 | | if (s == null) |
| | | 2517 | | { |
| | 0 | 2518 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); |
| | | 2519 | | } |
| | | 2520 | | |
| | 23836 | 2521 | | return Base64.DecodeFromChars(s); |
| | | 2522 | | } |
| | | 2523 | | |
| | | 2524 | | public static bool TryFromBase64String(string s, Span<byte> bytes, out int bytesWritten) |
| | | 2525 | | { |
| | 0 | 2526 | | if (s == null) |
| | | 2527 | | { |
| | 0 | 2528 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); |
| | | 2529 | | } |
| | | 2530 | | |
| | 0 | 2531 | | return TryFromBase64Chars(s.AsSpan(), bytes, out bytesWritten); |
| | | 2532 | | } |
| | | 2533 | | |
| | | 2534 | | public static bool TryFromBase64Chars(ReadOnlySpan<char> chars, Span<byte> bytes, out int bytesWritten) |
| | | 2535 | | { |
| | 0 | 2536 | | OperationStatus status = Base64.DecodeFromChars(chars, bytes, out _, out bytesWritten); |
| | 0 | 2537 | | if (status == OperationStatus.Done) |
| | | 2538 | | { |
| | 0 | 2539 | | return true; |
| | | 2540 | | } |
| | | 2541 | | |
| | 0 | 2542 | | bytesWritten = 0; |
| | 0 | 2543 | | return false; |
| | | 2544 | | } |
| | | 2545 | | |
| | | 2546 | | /// <summary> |
| | | 2547 | | /// Converts the specified range of a Char array, which encodes binary data as Base64 digits, to the equivalent |
| | | 2548 | | /// </summary> |
| | | 2549 | | /// <param name="inArray">Chars representing Base64 encoding characters</param> |
| | | 2550 | | /// <param name="offset">A position within the input array.</param> |
| | | 2551 | | /// <param name="length">Number of element to convert.</param> |
| | | 2552 | | /// <returns>The array of bytes represented by the specified Base64 encoding characters.</returns> |
| | | 2553 | | public static byte[] FromBase64CharArray(char[] inArray, int offset, int length) |
| | | 2554 | | { |
| | 23836 | 2555 | | ArgumentNullException.ThrowIfNull(inArray); |
| | 23836 | 2556 | | ArgumentOutOfRangeException.ThrowIfNegative(length); |
| | 23836 | 2557 | | ArgumentOutOfRangeException.ThrowIfNegative(offset); |
| | 23836 | 2558 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(offset, inArray.Length - length); |
| | | 2559 | | |
| | 23836 | 2560 | | return Base64.DecodeFromChars(new ReadOnlySpan<char>(inArray, offset, length)); |
| | | 2561 | | } |
| | | 2562 | | |
| | | 2563 | | /// <summary>Converts the specified string, which encodes binary data as hex characters, to an equivalent 8-bit |
| | | 2564 | | /// <param name="s">The string to convert.</param> |
| | | 2565 | | /// <returns>An array of 8-bit unsigned integers that is equivalent to <paramref name="s"/>.</returns> |
| | | 2566 | | /// <exception cref="ArgumentNullException"><paramref name="s"/> is <code>null</code>.</exception> |
| | | 2567 | | /// <exception cref="FormatException">The length of <paramref name="s"/>, is not zero or a multiple of 2.</excep |
| | | 2568 | | /// <exception cref="FormatException">The format of <paramref name="s"/> is invalid. <paramref name="s"/> contai |
| | | 2569 | | public static byte[] FromHexString(string s) |
| | | 2570 | | { |
| | 0 | 2571 | | if (s is null) |
| | | 2572 | | { |
| | 0 | 2573 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); |
| | | 2574 | | } |
| | 0 | 2575 | | return FromHexString(s.AsSpan()); |
| | | 2576 | | } |
| | | 2577 | | |
| | | 2578 | | /// <summary>Converts the span, which encodes binary data as hex characters, to an equivalent 8-bit unsigned int |
| | | 2579 | | /// <param name="chars">The span to convert.</param> |
| | | 2580 | | /// <returns>An array of 8-bit unsigned integers that is equivalent to <paramref name="chars"/>.</returns> |
| | | 2581 | | /// <exception cref="FormatException">The length of <paramref name="chars"/>, is not zero or a multiple of 2.</e |
| | | 2582 | | /// <exception cref="FormatException">The format of <paramref name="chars"/> is invalid. <paramref name="chars"/ |
| | | 2583 | | public static byte[] FromHexString(ReadOnlySpan<char> chars) |
| | | 2584 | | { |
| | 0 | 2585 | | if (chars.Length == 0) |
| | | 2586 | | { |
| | 0 | 2587 | | return []; |
| | | 2588 | | } |
| | | 2589 | | |
| | 0 | 2590 | | if (!int.IsEvenInteger(chars.Length)) |
| | | 2591 | | { |
| | 0 | 2592 | | ThrowHelper.ThrowFormatException_BadHexLength(); |
| | | 2593 | | } |
| | | 2594 | | |
| | 0 | 2595 | | byte[] result = GC.AllocateUninitializedArray<byte>(chars.Length / 2); |
| | | 2596 | | |
| | 0 | 2597 | | if (!HexConverter.TryDecodeFromUtf16(chars, result, out _)) |
| | | 2598 | | { |
| | 0 | 2599 | | ThrowHelper.ThrowFormatException_BadHexChar(); |
| | | 2600 | | } |
| | 0 | 2601 | | return result; |
| | | 2602 | | } |
| | | 2603 | | |
| | | 2604 | | /// <summary>Converts the span, which encodes binary data as hex characters, to an equivalent 8-bit unsigned int |
| | | 2605 | | /// <param name="utf8Source">The UTF-8 span to convert.</param> |
| | | 2606 | | /// <returns>An array of 8-bit unsigned integers that is equivalent to <paramref name="utf8Source"/>.</returns> |
| | | 2607 | | /// <exception cref="FormatException">The length of <paramref name="utf8Source"/>, is not zero or a multiple of |
| | | 2608 | | /// <exception cref="FormatException">The format of <paramref name="utf8Source"/> is invalid. <paramref name="ut |
| | | 2609 | | public static byte[] FromHexString(ReadOnlySpan<byte> utf8Source) |
| | | 2610 | | { |
| | 0 | 2611 | | if (utf8Source.Length == 0) |
| | | 2612 | | { |
| | 0 | 2613 | | return []; |
| | | 2614 | | } |
| | | 2615 | | |
| | 0 | 2616 | | if (!int.IsEvenInteger(utf8Source.Length)) |
| | | 2617 | | { |
| | 0 | 2618 | | ThrowHelper.ThrowFormatException_BadHexLength(); |
| | | 2619 | | } |
| | | 2620 | | |
| | 0 | 2621 | | byte[] result = GC.AllocateUninitializedArray<byte>(utf8Source.Length / 2); |
| | | 2622 | | |
| | 0 | 2623 | | if (!HexConverter.TryDecodeFromUtf8(utf8Source, result, out _)) |
| | | 2624 | | { |
| | 0 | 2625 | | ThrowHelper.ThrowFormatException_BadHexChar(); |
| | | 2626 | | } |
| | 0 | 2627 | | return result; |
| | | 2628 | | } |
| | | 2629 | | |
| | | 2630 | | /// <summary>Converts the string, which encodes binary data as hex characters, to an equivalent 8-bit unsigned i |
| | | 2631 | | /// <param name="source">The string to convert.</param> |
| | | 2632 | | /// <param name="destination"> |
| | | 2633 | | /// The span in which to write the converted 8-bit unsigned integers. When this method returns value different t |
| | | 2634 | | /// either the span remains unmodified or contains an incomplete conversion of <paramref name="source"/>, |
| | | 2635 | | /// up to the last valid character. |
| | | 2636 | | /// </param> |
| | | 2637 | | /// <param name="bytesWritten">When this method returns, contains the number of bytes that were written to <para |
| | | 2638 | | /// <param name="charsConsumed">When this method returns, contains the number of characters that were consumed f |
| | | 2639 | | /// <returns>An <see cref="OperationStatus"/> describing the result of the operation.</returns> |
| | | 2640 | | /// <exception cref="ArgumentNullException">Passed string <paramref name="source"/> is null.</exception> |
| | | 2641 | | public static OperationStatus FromHexString(string source, Span<byte> destination, out int charsConsumed, out in |
| | | 2642 | | { |
| | 0 | 2643 | | ArgumentNullException.ThrowIfNull(source); |
| | 0 | 2644 | | return FromHexString(source.AsSpan(), destination, out charsConsumed, out bytesWritten); |
| | | 2645 | | } |
| | | 2646 | | |
| | | 2647 | | /// <summary>Converts the span of chars, which encodes binary data as hex characters, to an equivalent 8-bit uns |
| | | 2648 | | /// <param name="source">The span to convert.</param> |
| | | 2649 | | /// <param name="destination"> |
| | | 2650 | | /// The span in which to write the converted 8-bit unsigned integers. When this method returns value different t |
| | | 2651 | | /// either the span remains unmodified or contains an incomplete conversion of <paramref name="source"/>, |
| | | 2652 | | /// up to the last valid character. |
| | | 2653 | | /// </param> |
| | | 2654 | | /// <param name="bytesWritten">When this method returns, contains the number of bytes that were written to <para |
| | | 2655 | | /// <param name="charsConsumed">When this method returns, contains the number of characters that were consumed f |
| | | 2656 | | /// <returns>An <see cref="OperationStatus"/> describing the result of the operation.</returns> |
| | | 2657 | | public static OperationStatus FromHexString(ReadOnlySpan<char> source, Span<byte> destination, out int charsCons |
| | | 2658 | | { |
| | 0 | 2659 | | (int quotient, int remainder) = Math.DivRem(source.Length, 2); |
| | | 2660 | | |
| | 0 | 2661 | | if (quotient == 0) |
| | | 2662 | | { |
| | 0 | 2663 | | charsConsumed = 0; |
| | 0 | 2664 | | bytesWritten = 0; |
| | | 2665 | | |
| | 0 | 2666 | | return (remainder == 1) ? OperationStatus.NeedMoreData : OperationStatus.Done; |
| | | 2667 | | } |
| | | 2668 | | |
| | | 2669 | | OperationStatus result; |
| | | 2670 | | |
| | 0 | 2671 | | if (destination.Length < quotient) |
| | | 2672 | | { |
| | 0 | 2673 | | source = source.Slice(0, destination.Length * 2); |
| | 0 | 2674 | | quotient = destination.Length; |
| | 0 | 2675 | | result = OperationStatus.DestinationTooSmall; |
| | | 2676 | | } |
| | | 2677 | | else |
| | | 2678 | | { |
| | 0 | 2679 | | if (remainder == 1) |
| | | 2680 | | { |
| | 0 | 2681 | | source = source.Slice(0, source.Length - 1); |
| | 0 | 2682 | | result = OperationStatus.NeedMoreData; |
| | | 2683 | | } |
| | | 2684 | | else |
| | | 2685 | | { |
| | 0 | 2686 | | result = OperationStatus.Done; |
| | | 2687 | | } |
| | | 2688 | | |
| | 0 | 2689 | | destination = destination.Slice(0, quotient); |
| | | 2690 | | } |
| | | 2691 | | |
| | 0 | 2692 | | if (!HexConverter.TryDecodeFromUtf16(source, destination, out charsConsumed)) |
| | | 2693 | | { |
| | 0 | 2694 | | bytesWritten = charsConsumed / 2; |
| | 0 | 2695 | | return OperationStatus.InvalidData; |
| | | 2696 | | } |
| | | 2697 | | |
| | 0 | 2698 | | bytesWritten = quotient; |
| | 0 | 2699 | | charsConsumed = source.Length; |
| | 0 | 2700 | | return result; |
| | | 2701 | | } |
| | | 2702 | | |
| | | 2703 | | /// <summary>Converts the span of UTF-8 chars, which encodes binary data as hex characters, to an equivalent 8-b |
| | | 2704 | | /// <param name="utf8Source">The span to convert.</param> |
| | | 2705 | | /// <param name="destination"> |
| | | 2706 | | /// The span in which to write the converted 8-bit unsigned integers. When this method returns value different t |
| | | 2707 | | /// either the span remains unmodified or contains an incomplete conversion of <paramref name="utf8Source"/>, |
| | | 2708 | | /// up to the last valid character. |
| | | 2709 | | /// </param> |
| | | 2710 | | /// <param name="bytesWritten">When this method returns, contains the number of bytes that were written to <para |
| | | 2711 | | /// <param name="bytesConsumed">When this method returns, contains the number of bytes that were consumed from < |
| | | 2712 | | /// <returns>An <see cref="OperationStatus"/> describing the result of the operation.</returns> |
| | | 2713 | | public static OperationStatus FromHexString(ReadOnlySpan<byte> utf8Source, Span<byte> destination, out int bytes |
| | | 2714 | | { |
| | 0 | 2715 | | (int quotient, int remainder) = Math.DivRem(utf8Source.Length, 2); |
| | | 2716 | | |
| | 0 | 2717 | | if (quotient == 0) |
| | | 2718 | | { |
| | 0 | 2719 | | bytesConsumed = 0; |
| | 0 | 2720 | | bytesWritten = 0; |
| | | 2721 | | |
| | 0 | 2722 | | return (remainder == 1) ? OperationStatus.NeedMoreData : OperationStatus.Done; |
| | | 2723 | | } |
| | | 2724 | | |
| | | 2725 | | OperationStatus result; |
| | | 2726 | | |
| | 0 | 2727 | | if (destination.Length < quotient) |
| | | 2728 | | { |
| | 0 | 2729 | | utf8Source = utf8Source.Slice(0, destination.Length * 2); |
| | 0 | 2730 | | quotient = destination.Length; |
| | 0 | 2731 | | result = OperationStatus.DestinationTooSmall; |
| | | 2732 | | } |
| | | 2733 | | else |
| | | 2734 | | { |
| | 0 | 2735 | | if (remainder == 1) |
| | | 2736 | | { |
| | 0 | 2737 | | utf8Source = utf8Source.Slice(0, utf8Source.Length - 1); |
| | 0 | 2738 | | result = OperationStatus.NeedMoreData; |
| | | 2739 | | } |
| | | 2740 | | else |
| | | 2741 | | { |
| | 0 | 2742 | | result = OperationStatus.Done; |
| | | 2743 | | } |
| | | 2744 | | |
| | 0 | 2745 | | destination = destination.Slice(0, quotient); |
| | | 2746 | | } |
| | | 2747 | | |
| | 0 | 2748 | | if (!HexConverter.TryDecodeFromUtf8(utf8Source, destination, out bytesConsumed)) |
| | | 2749 | | { |
| | 0 | 2750 | | bytesWritten = bytesConsumed / 2; |
| | 0 | 2751 | | return OperationStatus.InvalidData; |
| | | 2752 | | } |
| | | 2753 | | |
| | 0 | 2754 | | bytesWritten = quotient; |
| | 0 | 2755 | | bytesConsumed = utf8Source.Length; |
| | 0 | 2756 | | return result; |
| | | 2757 | | } |
| | | 2758 | | |
| | | 2759 | | /// <summary>Converts an array of 8-bit unsigned integers to its equivalent string representation that is encode |
| | | 2760 | | /// <param name="inArray">An array of 8-bit unsigned integers.</param> |
| | | 2761 | | /// <returns>The string representation in hex of the elements in <paramref name="inArray"/>.</returns> |
| | | 2762 | | /// <exception cref="ArgumentNullException"><paramref name="inArray"/> is <code>null</code>.</exception> |
| | | 2763 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="inArray"/> is too large to be encoded.</except |
| | | 2764 | | public static string ToHexString(byte[] inArray) |
| | | 2765 | | { |
| | 0 | 2766 | | ArgumentNullException.ThrowIfNull(inArray); |
| | 0 | 2767 | | return ToHexString(new ReadOnlySpan<byte>(inArray)); |
| | | 2768 | | } |
| | | 2769 | | |
| | | 2770 | | /// <summary> |
| | | 2771 | | /// Converts a subset of an array of 8-bit unsigned integers to its equivalent string representation that is enc |
| | | 2772 | | /// Parameters specify the subset as an offset in the input array and the number of elements in the array to con |
| | | 2773 | | /// </summary> |
| | | 2774 | | /// <param name="inArray">An array of 8-bit unsigned integers.</param> |
| | | 2775 | | /// <param name="offset">An offset in <paramref name="inArray"/>.</param> |
| | | 2776 | | /// <param name="length">The number of elements of <paramref name="inArray"/> to convert.</param> |
| | | 2777 | | /// <returns>The string representation in hex of <paramref name="length"/> elements of <paramref name="inArray"/ |
| | | 2778 | | /// <exception cref="ArgumentNullException"><paramref name="inArray"/> is <code>null</code>.</exception> |
| | | 2779 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> or <paramref name="length"/> is nega |
| | | 2780 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> plus <paramref name="length"/> is gr |
| | | 2781 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="inArray"/> is too large to be encoded.</except |
| | | 2782 | | public static string ToHexString(byte[] inArray, int offset, int length) |
| | | 2783 | | { |
| | 0 | 2784 | | ArgumentNullException.ThrowIfNull(inArray); |
| | | 2785 | | |
| | 0 | 2786 | | ArgumentOutOfRangeException.ThrowIfNegative(length); |
| | 0 | 2787 | | ArgumentOutOfRangeException.ThrowIfNegative(offset); |
| | 0 | 2788 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(offset, inArray.Length - length); |
| | | 2789 | | |
| | 0 | 2790 | | return ToHexString(new ReadOnlySpan<byte>(inArray, offset, length)); |
| | | 2791 | | } |
| | | 2792 | | |
| | | 2793 | | /// <summary>Converts a span of 8-bit unsigned integers to its equivalent string representation that is encoded |
| | | 2794 | | /// <param name="bytes">A span of 8-bit unsigned integers.</param> |
| | | 2795 | | /// <returns>The string representation in hex of the elements in <paramref name="bytes"/>.</returns> |
| | | 2796 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="bytes"/> is too large to be encoded.</exceptio |
| | | 2797 | | public static string ToHexString(ReadOnlySpan<byte> bytes) |
| | | 2798 | | { |
| | 0 | 2799 | | if (bytes.Length == 0) |
| | | 2800 | | { |
| | 0 | 2801 | | return string.Empty; |
| | | 2802 | | } |
| | 0 | 2803 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(bytes.Length, int.MaxValue / 2, nameof(bytes)); |
| | | 2804 | | |
| | 0 | 2805 | | return HexConverter.ToString(bytes, HexConverter.Casing.Upper); |
| | | 2806 | | } |
| | | 2807 | | |
| | | 2808 | | /// <summary>Converts a span of 8-bit unsigned integers to its equivalent span representation that is encoded wi |
| | | 2809 | | /// <param name="source">A span of 8-bit unsigned integers.</param> |
| | | 2810 | | /// <param name="destination">The span representation in hex of the elements in <paramref name="source"/>.</para |
| | | 2811 | | /// <param name="charsWritten">When this method returns, contains the number of chars that were written in <para |
| | | 2812 | | /// <returns>true if the conversion was successful; otherwise, false.</returns> |
| | | 2813 | | public static bool TryToHexString(ReadOnlySpan<byte> source, Span<char> destination, out int charsWritten) |
| | | 2814 | | { |
| | 0 | 2815 | | if (source.Length == 0) |
| | | 2816 | | { |
| | 0 | 2817 | | charsWritten = 0; |
| | 0 | 2818 | | return true; |
| | | 2819 | | } |
| | 0 | 2820 | | else if ((source.Length > (int.MaxValue / 2)) || (destination.Length < (source.Length * 2))) |
| | | 2821 | | { |
| | 0 | 2822 | | charsWritten = 0; |
| | 0 | 2823 | | return false; |
| | | 2824 | | } |
| | | 2825 | | |
| | 0 | 2826 | | HexConverter.EncodeToUtf16(source, destination); |
| | 0 | 2827 | | charsWritten = source.Length * 2; |
| | 0 | 2828 | | return true; |
| | | 2829 | | } |
| | | 2830 | | |
| | | 2831 | | /// <summary>Converts a span of 8-bit unsigned integers to its equivalent UTF-8 span representation that is enco |
| | | 2832 | | /// <param name="source">A span of 8-bit unsigned integers.</param> |
| | | 2833 | | /// <param name="utf8Destination">The UTF-8 span representation in hex of the elements in <paramref name="source |
| | | 2834 | | /// <param name="bytesWritten">When this method returns, contains the number of bytes that were written in <para |
| | | 2835 | | /// <returns>true if the conversion was successful; otherwise, false.</returns> |
| | | 2836 | | public static bool TryToHexString(ReadOnlySpan<byte> source, Span<byte> utf8Destination, out int bytesWritten) |
| | | 2837 | | { |
| | 0 | 2838 | | if (source.Length == 0) |
| | | 2839 | | { |
| | 0 | 2840 | | bytesWritten = 0; |
| | 0 | 2841 | | return true; |
| | | 2842 | | } |
| | 0 | 2843 | | else if ((source.Length > (int.MaxValue / 2)) || (utf8Destination.Length < (source.Length * 2))) |
| | | 2844 | | { |
| | 0 | 2845 | | bytesWritten = 0; |
| | 0 | 2846 | | return false; |
| | | 2847 | | } |
| | | 2848 | | |
| | 0 | 2849 | | HexConverter.EncodeToUtf8(source, utf8Destination); |
| | 0 | 2850 | | bytesWritten = source.Length * 2; |
| | 0 | 2851 | | return true; |
| | | 2852 | | } |
| | | 2853 | | |
| | | 2854 | | /// <summary>Converts an array of 8-bit unsigned integers to its equivalent string representation that is encode |
| | | 2855 | | /// <param name="inArray">An array of 8-bit unsigned integers.</param> |
| | | 2856 | | /// <returns>The string representation in hex of the elements in <paramref name="inArray"/>.</returns> |
| | | 2857 | | /// <exception cref="ArgumentNullException"><paramref name="inArray"/> is <code>null</code>.</exception> |
| | | 2858 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="inArray"/> is too large to be encoded.</except |
| | | 2859 | | public static string ToHexStringLower(byte[] inArray) |
| | | 2860 | | { |
| | 0 | 2861 | | ArgumentNullException.ThrowIfNull(inArray); |
| | 0 | 2862 | | return ToHexStringLower(new ReadOnlySpan<byte>(inArray)); |
| | | 2863 | | } |
| | | 2864 | | |
| | | 2865 | | /// <summary> |
| | | 2866 | | /// Converts a subset of an array of 8-bit unsigned integers to its equivalent string representation that is enc |
| | | 2867 | | /// Parameters specify the subset as an offset in the input array and the number of elements in the array to con |
| | | 2868 | | /// </summary> |
| | | 2869 | | /// <param name="inArray">An array of 8-bit unsigned integers.</param> |
| | | 2870 | | /// <param name="offset">An offset in <paramref name="inArray"/>.</param> |
| | | 2871 | | /// <param name="length">The number of elements of <paramref name="inArray"/> to convert.</param> |
| | | 2872 | | /// <returns>The string representation in hex of <paramref name="length"/> elements of <paramref name="inArray"/ |
| | | 2873 | | /// <exception cref="ArgumentNullException"><paramref name="inArray"/> is <code>null</code>.</exception> |
| | | 2874 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> or <paramref name="length"/> is nega |
| | | 2875 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> plus <paramref name="length"/> is gr |
| | | 2876 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="inArray"/> is too large to be encoded.</except |
| | | 2877 | | public static string ToHexStringLower(byte[] inArray, int offset, int length) |
| | | 2878 | | { |
| | 0 | 2879 | | ArgumentNullException.ThrowIfNull(inArray); |
| | | 2880 | | |
| | 0 | 2881 | | ArgumentOutOfRangeException.ThrowIfNegative(length); |
| | 0 | 2882 | | ArgumentOutOfRangeException.ThrowIfNegative(offset); |
| | 0 | 2883 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(offset, inArray.Length - length); |
| | | 2884 | | |
| | 0 | 2885 | | return ToHexStringLower(new ReadOnlySpan<byte>(inArray, offset, length)); |
| | | 2886 | | } |
| | | 2887 | | |
| | | 2888 | | /// <summary>Converts a span of 8-bit unsigned integers to its equivalent string representation that is encoded |
| | | 2889 | | /// <param name="bytes">A span of 8-bit unsigned integers.</param> |
| | | 2890 | | /// <returns>The string representation in hex of the elements in <paramref name="bytes"/>.</returns> |
| | | 2891 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="bytes"/> is too large to be encoded.</exceptio |
| | | 2892 | | public static string ToHexStringLower(ReadOnlySpan<byte> bytes) |
| | | 2893 | | { |
| | 0 | 2894 | | if (bytes.Length == 0) |
| | | 2895 | | { |
| | 0 | 2896 | | return string.Empty; |
| | | 2897 | | } |
| | 0 | 2898 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(bytes.Length, int.MaxValue / 2, nameof(bytes)); |
| | | 2899 | | |
| | 0 | 2900 | | return HexConverter.ToString(bytes, HexConverter.Casing.Lower); |
| | | 2901 | | } |
| | | 2902 | | |
| | | 2903 | | /// <summary>Converts a span of 8-bit unsigned integers to its equivalent span representation that is encoded wi |
| | | 2904 | | /// <param name="source">A span of 8-bit unsigned integers.</param> |
| | | 2905 | | /// <param name="destination">The span representation in hex of the elements in <paramref name="source"/>.</para |
| | | 2906 | | /// <param name="charsWritten">When this method returns, contains the number of chars that were written in <para |
| | | 2907 | | /// <returns>true if the conversion was successful; otherwise, false.</returns> |
| | | 2908 | | public static bool TryToHexStringLower(ReadOnlySpan<byte> source, Span<char> destination, out int charsWritten) |
| | | 2909 | | { |
| | 0 | 2910 | | if (source.Length == 0) |
| | | 2911 | | { |
| | 0 | 2912 | | charsWritten = 0; |
| | 0 | 2913 | | return true; |
| | | 2914 | | } |
| | 0 | 2915 | | else if ((source.Length > (int.MaxValue / 2)) || (destination.Length < (source.Length * 2))) |
| | | 2916 | | { |
| | 0 | 2917 | | charsWritten = 0; |
| | 0 | 2918 | | return false; |
| | | 2919 | | } |
| | | 2920 | | |
| | 0 | 2921 | | HexConverter.EncodeToUtf16(source, destination, HexConverter.Casing.Lower); |
| | 0 | 2922 | | charsWritten = source.Length * 2; |
| | 0 | 2923 | | return true; |
| | | 2924 | | } |
| | | 2925 | | |
| | | 2926 | | /// <summary>Converts a span of 8-bit unsigned integers to its equivalent UTF-8 span representation that is enco |
| | | 2927 | | /// <param name="source">A span of 8-bit unsigned integers.</param> |
| | | 2928 | | /// <param name="utf8Destination">The UTF-8 span representation in hex of the elements in <paramref name="source |
| | | 2929 | | /// <param name="bytesWritten">When this method returns, contains the number of bytes that were written in <para |
| | | 2930 | | /// <returns>true if the conversion was successful; otherwise, false.</returns> |
| | | 2931 | | public static bool TryToHexStringLower(ReadOnlySpan<byte> source, Span<byte> utf8Destination, out int bytesWritt |
| | | 2932 | | { |
| | 0 | 2933 | | if (source.Length == 0) |
| | | 2934 | | { |
| | 0 | 2935 | | bytesWritten = 0; |
| | 0 | 2936 | | return true; |
| | | 2937 | | } |
| | 0 | 2938 | | else if ((source.Length > (int.MaxValue / 2)) || (utf8Destination.Length < (source.Length * 2))) |
| | | 2939 | | { |
| | 0 | 2940 | | bytesWritten = 0; |
| | 0 | 2941 | | return false; |
| | | 2942 | | } |
| | | 2943 | | |
| | 0 | 2944 | | HexConverter.EncodeToUtf8(source, utf8Destination, HexConverter.Casing.Lower); |
| | 0 | 2945 | | bytesWritten = source.Length * 2; |
| | 0 | 2946 | | return true; |
| | | 2947 | | } |
| | | 2948 | | } |
| | | 2949 | | } |