C # basic (8) -- C # data type conversion,

Source: Internet
Author: User

C # basic (8) -- C # data type conversion,

C # data types can be converted in the following ways:

1,Forced conversionNote: The char type cannot be forcibly converted to int type. If forced conversion is used, the ASCII value of the original integer is obtained.

2,Class. parse (string type variable), can only be converted to a string, any type of data can be converted to a string by using the. tostring () method, and then using the "classType. Parse (string type variable)"To the desired type.To use this method, you can only process string content, and the string content can only be within the range that can be expressed by the target type.

3,Int. TryParse (string s, out int result)

This method also converts the numeric content string to the int type, but this method is superior to int. Parse, that is, it will not encounter exceptions. If the conversion is successful, true is returned. If the conversion fails, false is returned. Obviously, the last parameter is the output value. If the conversion fails, the output value is 0. If the conversion is successful, the corresponding value is output.

4,Convent Conversion;For example, Convert. ToInt32 (string or another type );This method is used to round the floating point number.. This method is the same as forced conversion. It cannot be used to process the char type. Otherwise, ASCII code is returned.


Symbol in C Language <Yes

Left shift operator (<)

Removes all the binary bits of an operation object from the left and adds 0 to the right ).

For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,

Move 1 to the left and then a = a * 2;

If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)

Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.

The operand shifts one digit to the right, which is equivalent to dividing the number by 2.

For example, a = a> 2 shifts the binary bit of a two places to the right,

0 or 1 to see whether the number is positive or negative.

Symbol in C Language <Yes

Left shift operator (<)

Removes all the binary bits of an operation object from the left and adds 0 to the right ).

For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,

Move 1 to the left and then a = a * 2;

If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)

Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.

The operand shifts one digit to the right, which is equivalent to dividing the number by 2.

For example, a = a> 2 shifts the binary bit of a two places to the right,

0 or 1 to see whether the number is positive or negative.

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.