[Essay] calculates the number of bytes of the half-width/full-width string (Appendix: Mind reader answer)

Source: Internet
Author: User

I haven't had a blog for half a month. I am busy with my work. Thank you for your attention.
There are two things to do today. The first is to announce the answer to the last "interesting mind-reading.
The second is to paste two functions used in the work to calculate the number of bytes of the half-width/full-width string.

(1) answer to "interesting mind reader:
Original post address: Interesting reading
Assuming that the number you selected is XY, the final result is 10 * X + Y-(x + y) = 9 * X, that is, no matter you choose, the final result must be a multiple of 9, that is, one of 9,18, 27,36, 45,54, 63,72, 81. Every time you click, the image corresponding to each number changes once. This gives you an illusion that the answer is not certain, but the image corresponding to the number changes no matter what, 9, 18, the images corresponding to 27,36, 45,54, 63,72, and 81 are the same. So of course it is what you think, because no matter how many XY you choose, it will be the answer.

(2) Calculate the byte count of the halfwidth/fullwidth string

Method 1:
String. Prototype. bytes = function (){
VaR Carr = This. Match (/[^/x00-/xFF]/ig );
Return this. Length + (CARR = NULL? 0: Carr. Length );
}

// Call
// The maximum number of incoming Force grids is 255.
VaR comment = Document. getelementbyid ("delicomment ");
If (comment. value. bytes ()> 255 ){
Alert. ');
Comment. Focus ();
Return;
}
Bytes -----------------------------------------------------------------------------------------------------
Method 2:
Function strlen_all (STR ){
VaR Len = 0;
For (VAR I = 0; I <Str. length; I ++ ){
If (Str. charcodeat (I)> 255)
Len + = 2;
Else
Len ++;
}
Return Len;
}

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.