C #. NET type conversion

Source: Internet
Author: User

C # has been coming out for some days. Recently, due to programming needs, I have made some research on C # type conversion, the content involves C # Packing/unpacking/alias, mutual conversion between numeric types, ASCII and Unicode characters, conversion between numeric strings and numeric values, string and character array/byte conversion between arrays, conversion between various numeric types and byte arrays, hexadecimal output, and some Conversion Processing of date data, share with you here --

1. binning, unboxing, or Alias

In many C #. NET books, int-> Int32 is a packing process, and vice versa. The same applies to many other variable types, such as short <-> Int16, long <-> Int64. Generally, programmers do not need to understand this process, because these packing and unpacking actions can be completed automatically without code intervention. However, we need to remember the relationships between these types. Therefore, we use "aliases" to remember the relationships between them.
C # is a fully object-oriented language that is more thorough than Java's object-oriented language-it encapsulates a simple data type into a class through the default packing action. Int32, Int16, Int64, and so on are the corresponding class names, and the familiar and easy-to-remember names, such as int, short, and long, we can call it an alias of the Int32, Int16, Int64, and other types.
In addition to the three types, which classes have "alias? The following are commonly used:

Bool-> System. Boolean (Boolean type, its value is true or false)
Char-> System. Char (Bytes type, occupies two bytes, indicating 1 Unicode character)
Byte-> System. Byte (byte, which occupies 1 Byte, indicates an eight-digit positive integer, ranging from 0 ~ 255)
Sbyte-> System. SByte (in byte notation, which occupies 1 byte, indicates an 8-digit integer, range:-128 ~ 127)
Ushort-> System. UInt16 (unsigned short integer, which occupies 2 bytes and represents a 16-bit positive integer in the range of 0 ~ 65,535)
Uint-> System. UInt32 (unsigned integer, 4 bytes, representing a 32-bit positive integer, range: 0 ~ 4,294,967,295)
Ulong-> System. UInt64 (unsigned long integer, which occupies 8 bytes, indicates a 64-bit positive integer in the range of 0 ~ About 10 to the power of 20)
Short-> System. Int16 (short integer, which occupies 2 bytes, indicates a 16-bit integer, range:-32,768 ~ 32,767)
Int-> System. Int32 (integer, 4 bytes, representing a 32-bit integer, ranging from-2,147,483,648 to 2,147,483,647)
Long-> System. Int64 (an integer of 8 bytes, representing a 64-bit integer. The value range is about-(19 of 10) to the 19th power of 10)
Float-> System. Single (Single-precision floating point, 4 bytes)
Double-> System. Double (double-precision floating point type, 8 bytes)

We can use the following code for an experiment:

 

Code:Private void TestAlias (){
// This. textBox1 is a text box of the System. Windows. Forms. TextBox type.
// The Multiline attribute has been set to true in the design.
Byte a = 1; char B = a; short c = 1;
Int d = 2; long e = 3; uint f = 4; bool g = true;
This. textBox1.Text = "";
This. textBox1.AppendText ("byte->" + a. GetType (). FullName + "");
This. textBox1.AppendText ("char->" + B. GetType (). FullName + "");
This. textBox1.AppendText ("short->" + c. GetType (). FullName + "");
This. textBox1.AppendText ("int->" + d. GetType (). FullName + "");
This. textBox1.AppendText ("long->" + e. GetType (). FullName + "");
This. textBox1.AppendText ("uint->" + f. GetType (). FullName + "");
This. textBox1.AppendText ("bool->" + g. GetType (). FullName + "");
}



Create a new button in the form and call the TestAlias () function in its click event. The running result is as follows:

Byte-> System. Byte
Char-> System. Char
Short-> System. Int16
Int-> System. Int32
Long-> System. Int64
Uint-> System. UInt32
Bool-> System. Boolean

This is enough to indicate the classes corresponding to each alias!

2. Mutual conversion between numeric types

The value types mentioned here include byte, short, int, long, fload, and double. According to the order, values of various types can be automatically converted backward. For example, if a short type data value is assigned to an int type variable, the short value is automatically converted to an int type value and then assigned to the int type variable. For example:

 

Code:Private void TestBasic (){
Byte a = 1; short B = a; int c = B;
Long d = c; float e = d; double f = e;
This. textBox1.Text = "";
This. textBox1.AppendText ("byte a =" + a. ToString () + "");
This. textBox1.AppendText ("short B =" + B. ToString () + "");
This. textBox1.AppendText ("int c =" + c. ToString () + "");
This. textBox1.AppendText ("long d =" + d. ToString () + "");
This. textBox1.AppendText ("float e =" + e. ToString () + "");
This. textBox1.AppendText ("double f =" + f. ToString () + "");
}



The running result shows that the values of each variable are 1. Of course, their types are System. Byte ...... System. Double type. Now let's try. What if we reverse the order of values? Append the following statement to the TestBasic () function:

 

Code:Int g = 1;
Short h = g;
This. textBox1.AppendText ("h =" + h. ToString () + "");



Result compilation error:
  

Quote:G: ProjectsVisual C # ConvertForm1.cs (118): The type "int" cannot be implicitly converted to "short"


The first line of Form1.cs is the row where short h = g is located.

At this time, if we insist on conversion, we should use forced type conversion, which is often mentioned in C language, that is, "(type name) variable name to forcibly convert data. The preceding example is modified as follows:

 

Code:Short g = 1;
Byte h = (byte) g; // convert the value of the short Type g to the short type and then assign it to the variable h.
This. textBox1.AppendText ("h =" + h. ToString () + "");



After compilation, h = 1 is output and the conversion is successful.
However, if we use forced conversion, we have to consider another problem: the short type range is-32768 ~ 23767, while the byte type ranges from 0 ~ 255. What will happen if the variable g size exceeds the byte range? Let's rewrite the code again and change the value to 265, which is 10 larger than 255.

 

Code:Short g = 265; // 265 = 255 + 10
Byte h = (byte) g;
This. textBox1.AppendText ("h =" + h. ToString () + "");



There is no compilation error, but the running result is not h = 265, but h = 9.
Therefore, when performing conversion, we should note that the data to be converted cannot exceed the range of the target type. This is not only reflected in the conversion of the Multi-byte data type (relative, short in the above example) to the smaller byte type (relative, byte in the above example, it is also reflected in the conversion of byte 129 to sbyte between the same number of bytes of the signed type and the unsigned type. This example is similar and not detailed.

3. ASCII and Unicode characters

Most of the time, we need to get an ASCII code of an English character, a Unicode code of a Chinese character, or query the encoding of a character from the relevant encoding. Many people, especially those who learn C # From the VB program sequence, complain about why C # does not provide ready-made functions to do this-because there is Asc () in VB () functions and Chr () functions are used for this type of conversion.
However, if you have learned C, you will know that we only need to forcibly convert the English numeric data into appropriate numeric data to get the corresponding ASCII code. Otherwise, if you forcibly convert a suitable numeric data to numeric data, you can get the corresponding characters.
In C #, the character range is extended, including not only single-byte characters, but also double-byte characters, such as Chinese characters. The conversion between characters and encoding is still extended by the C language-forced conversion. Let's take a look at the example below.

 

Code:Private void TestChar (){
Char ch = a; short ii = 65;
This. textBox1.Text = "";
This. textBox1.AppendText ("The ASCII code of" + ch + "is:" + (short) ch + "");
This. textBox1.AppendText ("ASCII is" + ii. ToString () + ", the char is:" + (char) ii + "");
Char cn = medium; short uc = 22478;
Thi

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.