Char-type numeric conversion, char-type numeric Conversion

Source: Internet
Author: User

Char-type numeric conversion, char-type numeric Conversion
Char type numeric Conversion

In the video tutorial, you have learned how to convert numbers, strings, and other types. In some cases, we also need to convert the char type to the int type, or convert the int type to the char type.

This article will introduce the knowledge that needs to be learned, although not commonly used in the code.

  • Char type numeric Conversion
    • Convert char to int
    • Convert int to char
    • Character Data calculation
Convert char to int

One character'Han'How can it be converted to a number?

Actually, it is okay. As mentioned in the previous supplementary documents, the storage of characters by computers is stored by numbers corresponding to certain encoding rules.

In C #, Unicode encoding is used to store characters..

For example, Chinese characters'Han'The Unicode code is 27721. in computer memory, the binary code corresponding to Khan 27721 is used for storage.

Therefore'Han'Converts a number of the int type to 27721.

So what kind of code is used to complete the conversion?

Any data of the char type can be implicitly converted to the int type..

Since it is an implicit conversion, the following code is correct:

Char c = 'hangzhou ';
Int n = c;
Console. WriteLine (n );
N = 'a ';
Console. WriteLine (n );

Run the above Code and the output will be:

27721
97

Description'Han'And'A'The Unicode encoding types are 27721 and 97.

Why can I use implicit conversions to assign values to int-type variables for char data?

The reason is that char data is stored in the memory using Unicode encoding, Unicode encoding occupies 16 bits (two bytes), and int type occupies 32 bits (four bytes ). Therefore, char data occupies less space and the value range is small. int data occupies more space and the value range is large. Therefore, such a value assignment is safe and will not cause data loss. It can be implicitly converted.

Convert int to char

For example, I have an int type variable 27721. I want to see this number as the encoding. What is the corresponding character?

In this case, you need to convert int type data to char type.

Display conversion is required when int type data is converted to char type data..

Why must I use display conversion? If you understand how to convert char to int, you should understand the reason.

The value range of the char type is smaller than that of the int type. Therefore, it is unsafe to convert the int type with a large value range to a char type with a small value range. Therefore, display conversion is required.

The following code shows the characters represented by numbers 27721 and 97:

int n = 27721;
char c = (char)n;
Console.WriteLine(c);
c = (char)97;
Console.WriteLine(c);
Character Data calculation

When performing mathematical operations (+,-, *,/, and %) on char data, it is treated as an int. The return type of the operation is int..

That is to say, the character can also be involved in mathematical operations. During the operation, the encoding of the character is calculated, and the return result of the operation is an integer int.

For example, 'A' + '1', the Code calculates the encoding of 'a' plus '1', and returns an integer added to the encoding. The encoding of 'A' is 97, the encoding of '1' is 49, and the calculation result is an integer of the int type 146.

The following code can be used to describe this point.

int a = 'a'+'1';
Console.WriteLine(a);

146 is output after running.

For example, what will be output after the following code is run?

int a = 'a'+1;
char c = (char)a;
Console.WriteLine(c);

Analysis:

  • 'A' + 1 is to add 1 to the encoding of a, and the result is 98. Save 98 to variable.
  • Then, convert 98 to the corresponding character. The conversion result is the character 'B' and save it to variable c.
  • Finally, the output variable c Outputs the character B.

When performing mathematical operations (+,-, *,/, and %) on char data, it is treated as an int and the return type of the operation is int. This method is calledAutomatic type upgradeIn fact, not just char, the values return numeric types smaller than the int type, such as byte and short, they also apply to automatic escalation rules.

For example, when two bytes are used for mathematical operations, they are also considered as int operations, and the returned result is also int.

byte s1 = 2, s2 = 3;
byte s3 = s1 + s2;

The second code above will return an error, prompting you that int cannot be assigned to byte. It is because the two byte variables s1 and s2 are upgraded to the int type during mathematical operations. The calculation result is an int type, values cannot be implicitly converted to s3, a byte type variable.

The reason for doing so is because the values of a byte range are very small. If they are not upgraded, they often cause the results to exceed the value range During computation, and the excess is lost, the error data is returned. After being upgraded to an int type with a larger value range, this problem will be solved to a large extent.

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.