Study Notes on char length in Java and C/C ++

Source: Internet
Author: User

Study Notes on char length in java and c/c ++
In the past two days, I am helping you change an encryption program. The following is the core code of c ++:

Char cpass [5]; for (int j = 0; j <instring. length (); j ++) {cpass [j] ^ = npwd ;}

 

However, a problem may occur when writing data to java. At first glance, the conversion can be basically unblocked, but the result is problematic.

It is a char type problem found by searching online materials. In fact, this involves comparing the data types in c/c ++ with those in java.

First, the char type.

The char type in c/c ++ is 8-bit

The char type in java is 16-bit

 

Therefore, when processing this type of conversion using java, you should pay attention to clearing the 8-bit high of the char type, which is easy to match with the 0x00ff bit. The completed java program is as follows:

 

Char cpass [5]; for (int j = 0; j <instring. length (); j ++) {cpass [j] ^ = npwd; cpass [j] & = 0x00ff; // clear the 8-bit high}

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.