Get the App
SLTechnology News&Howtos  ›  Development  › 

How javascript converts a string to binary

Shulou Source: shulou.com Published: 2022-06-01 21:50:30 09月27日 Update

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)

Tags: Characters binaries strings arrays elements methods content articles articles code interests tutorials novice easy to understand more friends organization environment computer knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Redmi NVidia Xiaomi vpn