How javascript converts a string to binary
The content of this article mainly focuses on how javascript converts strings into binary. The content of the article is clear and well-organized. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!
Conversion methods: 1, the use of split () method to convert the string into a character array; 2, traversing the character array, the use of charCodeAt () and toString () method to convert each character element into a binary value; 3, the use of join () method to concatenate array elements, into a complete binary value.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
Javascript converts a string to binary
Convert a string to a character array
Iterate through the character array, using charCodeAt () to convert each character element to an ascii code
Use toString (2) to convert ascii code elements to binary
Use join () to concatenate array elements to binary strings.
Implementation code:
Function strToBinary (str) {var result = []; var list = str.split (""); for (var itemositani)