In-depth analysis on the internal mechanism of converting Char to int/unsigned int

Source: Internet
Author: User
Analysis of converting Char to int/unsigned int: Note: The trial environment is VC ++ 6.0. In VC ++ 6.0, char is regarded as signed Char, so the Integer Range that char can represent is-218 -- + 127 first analyzes the situation of converting to int: 1. If the range indicated by char is between-128 -- + 127, the value size remains unchanged when it is converted to an integer. For example: Char CH = I // I is an integer between-128 and + 127. Int inv = CH; // The value of inv is also an integer 2 between-128 and + 127. If the char expression range is not between-128 and + 127, in this case, take the lowest 8 and convert it to int, for example, char H = 128; int inv = CH; // at this time, the INV value is-128. Why is the result above-128? The analysis is as follows: H content in the memory is (from left to right is from high to low, the same below) 10000000 (in the form of complement code) Put the content in H into Inv, first, judge that the first digit is 1, it must be a negative number, and then convert 10000000 to the original code 10000000, so the INV value is-128. The following is an analysis of the conversion to unsigned INT: 1. If the char type variable value is 0-+ 127, after being converted to the int type, the numeric value remains unchanged. Char H = I // I is an integer between 0 and + 127. Int inv = H // the INV value is also an integer between 0 and + 127. 2. If the char type variable is negative, take 8 lower bits. If 8th is 1, add 1 for expansion. If 8th is 0, add 0 for expansion. For example, char H =-129 unsigned int inv = H. At this time, the INV value is + 127. Why? The analysis is as follows:-129 is written as binary 101111111 (complement representation), and the 8-bit lower value is 01111111. Because the eighth bit is 0, when it is extended to 32int, it is added as 0, so the INV value is + 127 char H =-1; unsigned int inv = h at this time, the INV value is 4294967295, that is, 2 ^ 32-1. Why? The analysis is as follows:-1 is written as binary 11111111 (complement representation), and the 8-bit lower is 11111111. Because the eighth bit is 1, when it is extended to 32int, 1 is added to the high position, therefore, the INV value is reduced by 1 to the power of 2, that is, 4294967295. Well, with the above analysis and examples, we have understood the internal mechanism of converting Char to int/unsigned Int. What about converting unsigned char to int/unsigned Int, I believe that with the above examples and analysis, readers can easily come up with the corresponding conversion mechanism. If you are interested, you can give it a try and I will not go into details here. The end, thanks very much. The final description is as follows: Indicate the author and the source if any reposted. The ownership of this blog belongs to the blog and the author.

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.