JavaScript in-process conversion

Source: Internet
Author: User

I. Explanation of grammar 1, parseint ()

parseint: Converting a string to an integer

parseint (string, radix)

    • String to be parsed.
    • Optional. Represents the cardinality of the number to parse. The value is between 2 and 36.

      If this argument is omitted or its value is 0, the number is parsed based on 10.
      If it starts with "0x" or "0X", it will have a base of 16.

      If the parameter is less than 2 or greater than 36, parseint () returns NaN.

2. ToString ()

The ToString () method belongs to the Object object, and many of the built-in objects of JavaScript rewrite the function to make it more suitable for its own functional needs.

type Behavior Description
Array Converts each element of the Array to a string and joins them in turn, stitching together two elements with commas as delimiters.
Boolean Returns "true" if the Boolean value is true. Otherwise, "false" is returned.
Date Returns the text representation of the date.
Error Returns a string containing the associated error information.
Function Returns a string of the following format, where functionname is the name of a function, and the toString method of this function is called: "function functionname () {[native code]}"
Number Returns a string representation of a numeric value. You can also return a string specified as a binary representation, refer to number.tostring ().
String Returns the value of a String object.
Object (default) Returns "[Object ObjectName]", where ObjectName is the name of the object type.
2. Binary conversion
//decimal to other binaryvarx=110;  alert (x); Alert (x.tostring (2)); Alert (x.tostring (8)); Alert (x.tostring (32)); Alert (x.tostring (16)); //Other Turn decimalvarx= ' 110 '; Alert (parseint (x,2));//6 = 2 binary parsingAlert (parseint (x,8));//the 8 binary analysisAlert (parseint (x,16));//272 = with 16 binary parsing//other transfer to other//use parseint to convert to decimal before using ToString to go to target systemAlert (String.fromCharCode (parseint (141,8)) alert (parseint ("FF", +). ToString (2));

Third, test questions

Write a function that takes an (unsigned) integer as input, and returns the number of bits that is equal to one in the BI Nary representation of that number.

Example: The binary representation 1234 10011010010 of IS, so the function should return in this case 5

<script>    function  countbits (n) {        var resultstring = n.tostring (2);         var l = resultstring.length;         var resultnum = 0;          for (var i = 0; i < l; i++) {            + = resultstring[i]-0;        }        Console.log (resultnum);         return resultnum;    }    Func (1234);     </script>

The author starof, because the knowledge itself in the change, the author is also constantly learning and growth, the content of the article is not updated regularly, in order to avoid misleading readers, convenient tracing, please reprint annotated source: http://www.cnblogs.com/starof/p/6693318. HTML has a problem welcome to discuss with me, common progress.

JavaScript in-process conversion

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.