Javascript method to convert a string to octal: 1. Use the parseInt() function to convert the string to a decimal number, the syntax is "parseInt("string")"; 2. Use toString() The function converts a decimal number to an octal number, the syntax is "decimal number.toString(8)".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Javascript method to convert a string to octal number
First use the parseInt() function to convert the string to a decimal number
Then use the toString() function to convert the decimal number to octal number
Example:
var str = "1254"; var num =parseInt(str); var x=num.toString(8); console.log(x);
Instructions:
1. parseInt()
parseInt: Convert a string into an integer
parseInt(string, radix)
Optional. Represents the base of the number to be parsed. The value is between 2 ~ 36.
If this parameter is omitted or its value is 0, the number will be parsed in base 10.
If it starts with "0x" or "0X", it will be base 16.
If the parameter is less than 2 or greater than 36, parseInt() will return NaN.
2. toString()
toString() method belongs to the Object
object. Many built-in objects in JavaScript have overridden this function to Achieve functional needs that are more suitable for you.
Type | Behavior description |
---|---|
Array | Convert each element of Array as strings, and concatenate them one after another, using commas as separators between the two elements for splicing. |
Boolean | If the Boolean value is true, returns "true". Otherwise return "false". |
Date | Returns the textual representation of the date. |
Error | Returns a string containing relevant error information. |
Function | Returns a string in 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 the string representation of the value. It can also return a string expressed in the specified base, please refer to Number.toString(). |
String | Returns the value of the String object. |
Object (default) | Returns "[object ObjectName]", where ObjectName is the name of the object type. |
[Related recommendations: javascript learning tutorial]
The above is the detailed content of How to convert string to octal in javascript. For more information, please follow other related articles on the PHP Chinese website!