# C ++ type conversion

Source: Internet
Author: User

Today, when studying character encoding, I accidentally think of a problem. In ansi encoding, a Chinese character is expressed in two bytes, but a char is a byte, so what will happen if I assign a Chinese character to a char variable and print the output?

I tried it and found garbled characters. unexpectedly, the computer still split the Chinese character into two halves and assigned a value to variable, which half is assigned to it?

After a try, you can see that the hexadecimal code of the Chinese character 'You' is C4 E3. The output result is as follows:

Char a = 'you ';
Cout <(int);

The output result is-29, and the result of E3 calculation is the same as the result, so we guess the last half is selected. Use 'me' for verification. C4 D2. The result is-46. The conjecture is correct.

In fact, there were two conversions in the above program. The first time we split your character into two halves, we assigned the last half to, the second time, because a is a byte and int Is four bytes, it is necessary to add zero before a to reach the length of four bytes.

If you assign an int value to the char variable, is it the last section? The following program shows that:

Int B = 7898;
Char c = B;
Cout <(int) c;

The result of the program is-38, and the binary code of 7898 is 1111011011010. After studying the last eight bits, we find that the results are consistent. Therefore, the computer still has the last eight bits, instead of the first eight bits.

So now we can think that the computer is always intercepted at the back, not at the front.

However, in a computer, the representation of floating point numbers is a little complicated, so the computer is also different when converting from floating point numbers to other types. For example, if you want to convert a floating point to an int integer, the computer intercepts the integer part instead of the last 32 digits. How about converting a floating point to a char type?

Double aa = 3000.9;
Char te = aa;
Cout <endl <(int) te;

The result is a binary representation of-72, which is exactly 3000. After multiple verification, it is found that the first type of floating point to char type is converted to int type, then convert int type to char type.

For more types of conversion, continue to follow this blog.

Related Keywords:
Related Article

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.