README.md (2647B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # array2buffer 22 23 > Allocate a [buffer][@stdlib/buffer/ctor] using an octet array. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 </section> 30 31 <!-- /.intro --> 32 33 <!-- Package usage documentation. --> 34 35 <section class="usage"> 36 37 ## Usage 38 39 ```javascript 40 var array2buffer = require( '@stdlib/buffer/from-array' ); 41 ``` 42 43 #### array2buffer( arr ) 44 45 Allocates a [buffer][@stdlib/buffer/ctor] using an `array` (or array-like `object`) of octets. 46 47 ```javascript 48 var buf = array2buffer( [ 1, 2, 3, 4 ] ); 49 // returns <Buffer>[ 1, 2, 3, 4 ] 50 ``` 51 52 </section> 53 54 <!-- /.usage --> 55 56 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 57 58 <section class="notes"> 59 60 </section> 61 62 <!-- /.notes --> 63 64 <!-- Package usage examples. --> 65 66 <section class="examples"> 67 68 ## Examples 69 70 <!-- eslint no-undef: "error" --> 71 72 ```javascript 73 var array2buffer = require( '@stdlib/buffer/from-array' ); 74 75 var octets; 76 var buf; 77 var str; 78 var i; 79 80 // Define a string we want to convert to a buffer: 81 str = 'this is a string.'; 82 83 // Manually convert the string to an array of octets... 84 octets = new Array( str.length ); 85 for ( i = 0; i < str.length; i++ ) { 86 octets[ i ] = str.charCodeAt( i ) % 256; 87 } 88 89 // Create a buffer from the octet array: 90 buf = array2buffer( octets ); 91 console.log( buf.toString() ); 92 ``` 93 94 </section> 95 96 <!-- /.examples --> 97 98 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 99 100 <section class="references"> 101 102 </section> 103 104 <!-- /.references --> 105 106 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 107 108 <section class="links"> 109 110 [@stdlib/buffer/ctor]: https://www.npmjs.com/package/@stdlib/buffer/tree/main/ctor 111 112 </section> 113 114 <!-- /.links -->