sistema binario octal decimal hexadecimal conversiones e
4 stars based on
Cada valor tiene un tipo asociado, que define los atributos como la cantidad de espacio asignado al valor, el intervalo de valores posibles que puede tener y los miembros que ofrece. Every value has an associated type, which defines attributes such as the amount of space allocated to the value, the range of possible values it can have, and the members that it makes available. Many values can be expressed as more than one type.
Por ejemplo, el valor 4 se puede expresar como un entero o como un valor de punto flotante. For example, the value 4 can be expressed as an integer or a floating-point value. Type conversion creates a value in a new type that is equivalent to the value of an old type, but does not necessarily preserve the identity or exact value of the original object. NET Framework automatically supports the following conversions: Conversion from a derived class to a base class.
Esto significa, por ejemplo, que una instancia de cualquier clase o estructura se puede convertir en una instancia de tipo Object. This means, for example, that an instance of any class or structure can be converted to an Object instance. This conversion does not require a casting or conversion operator. Conversion from a base class back to the original derived class. In Cthis conversion requires a casting operator. Conversion from a type that implements an interface to an interface object that represents that interface.
Conversion from an interface object back to the original type that implements that interface. In addition to these automatic conversions, the. NET Framework provides several features that support custom type conversion. Entre ellas se incluyen las siguientes: These include the following: The Implicit operator, which defines the available widening conversions between types.
For more information, see the Implicit Conversion with the Implicit Operator section. The Explicit operator, which defines the available narrowing conversions between types. For more information, see the Explicit Conversion with the Explicit Operator section. La interfaz IConvertibleque define las conversiones en cada uno de los tipos de datos base de. The IConvertible interface, which defines conversions to each of the base. NET Framework data types.
For more information, see The IConvertible Interface section. The Convert class, which provides a set of methods that implement the methods in the IConvertible interface. For more information, see The Convert Class section. The TypeConverter class, which is a base class that can be extended to support the conversion of a specified type to any other type.
For more information, see The TypeConverter Class section. Widening conversions involve the creation of a new value from the value of an existing type that has either a more restrictive range or a more restricted member list than the target type.
Widening conversions cannot result in data loss although they may result in a loss of precision. Because data cannot be lost, compilers can handle the conversion implicitly or transparently, without requiring the use of an explicit conversion method or a casting operator. Although code that performs an implicit conversion can call a conversion method or use a casting operator, their use is not required by compilers that support implicit conversions.
The following example illustrates some of these implicit conversions in assigning values to a Decimal variable. If a particular language compiler supports custom operators, you can also define implicit conversions in your own custom types.
The following example provides a partial implementation of a signed byte data type named ByteWithSign that uses sign-and-magnitude representation. Client code can then declare a ByteWithSign variable and assign it Byte and SByte values without performing any explicit conversions or using any casting operators, as the following example shows. Volver al principio Back to top.
Narrowing conversions involve the creation of a new value from the value of an existing type that has either a greater range or a larger member list than the target type. Because a narrowing conversion can result in a loss of data, compilers often require that the conversion be made explicit through a call to a conversion method or a casting operator. That is, the conversion must be handled explicitly in developer code.
The major purpose of requiring a conversion method or casting operator for narrowing conversions is to make the developer aware of the possibility of data loss or an OverflowException so that it can be handled in code. Sin embargo, algunos compiladores pueden ser menos exigentes con este requisito. However, some compilers can relax this requirement.
For example, in Visual Basic, if Option Strict is off its default settingthe Visual Basic compiler tries to perform narrowing conversions implicitly. Por ejemplo, los tipos de datos UInt32Int64 y UInt64 tienen intervalos que superan el del tipo de datos Int32como se muestra en la tabla siguiente. For example, the UInt32Int64and UInt64 data types have ranges that exceed that the Int32 data type, as the following table shows.
NET Framework permite que los tipos definan un operador Explicit. To handle such narrowing conversions, the. NET Framework allows types to define an Explicit operator.
Individual language compilers can then implement this operator using their own syntax, or a member of the Convert class can be called to perform the conversion.
For more information about the Convert class, see The Convert Class later in this topic. The following example illustrates the use of language features to handle the explicit conversion of these potentially out-of-range integer values to Int32 values.
Explicit conversions can produce different results in different languages, and these results can differ from the value returned by the corresponding Convert method. Por ejemplo, si el valor Double NET Framework redondean el valor Double para devolver un valor de 13, pero el operador int de C trunca Double para devolver un valor de For example, if the Double value ToInt32 Double method round the Double to return a value of 13, but the C int operator truncates the Double to return a value of Similarly, the C int operator does not support Boolean-to-integer conversion, but the Visual Basic CInt method converts a value of true to ToInt32 Boolean convierte un valor de true en 1.
On the other hand, the Convert. ToInt32 Boolean method converts a value of true to 1. Most compilers allow explicit conversions to be performed in a checked or unchecked manner. When a checked conversion is performed, an OverflowException is thrown when the value of the type to be converted is outside the range of the target type. When an unchecked conversion is performed under the same conditions, the conversion might not throw an exception, but the exact behavior becomes undefined and an incorrect value might result.
By default, explicit conversions are unchecked. By default, explicit conversions are checked. The following C example uses the checked and unchecked keywords to illustrate the difference in behavior when a value outside the range of a Byte is converted to a Byte. MaxValue a la variable Byte. The checked conversion throws an exception, but the unchecked conversion assigns Byte.
MaxValue to the Byte variable. If a particular language compiler supports custom overloaded operators, you can also define explicit conversions in your own custom types. Client code can then declare a ByteWithSign variable and assign it Int32 and UInt32 values if the assignments include a casting operator or a conversion method, as the following example shows.
To support the conversion of any type to a common language runtime base type, the. The implementing type is required to provide the following: A method that returns the TypeCode of the implementing type. Methods to convert the implementing type to each common language runtime base type BooleanByteDateTimeDecimalDoubleand so on. A generalized conversion method to convert an instance of the implementing type to another specified type.
Conversions that are not supported should throw an InvalidCastException. However, these are explicit interface implementations; the conversion method can be called only through an IConvertible interface variable, as the following example shows.
Este ejemplo convierte un valor Int32 en su valor Char equivalente. This example converts an Int32 value to its equivalent Char value.
The requirement to call the conversion method on its interface rather than on the implementing type makes explicit interface implementations relatively expensive. En su lugar, se recomienda llamar al miembro adecuado de la clase Convert para convertir entre los tipos base de Common Language Runtime. Instead, we recommend that you call the appropriate member of the Convert class to convert between common language runtime base types.
For more information, see the next section, The Convert Class. In addition to the IConvertible interface and the Convert class provided by the. NET Frameworkindividual languages may also provide ways to perform conversions. For the most part, the IConvertible interface is designed to support conversion between the base types in the. However, the interface can also be implemented by a custom type to support conversion of that type to other custom types.
For more information, see the section Custom Conversions with the ChangeType Method later in this topic. Convert es la manera recomendada de convertir de un tipo base en otro de una manera independiente del lenguaje. Although each base type's IConvertible interface implementation can be called to perform a type conversion, calling the methods of the System.
Convert class is the recommended language-neutral way to convert from one base type to another. In addition, the Convert. The Convert class provides a language-neutral way to perform conversions between base types and is available to all languages that target the common language runtime.
It provides a complete set of methods for both widening and narrowing conversions, and throws an InvalidCastException for conversions that are not supported such as the conversion of a DateTime value to an integer value.