README.md (3082B)
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 # string2buffer 22 23 > Allocate a [buffer][@stdlib/buffer/ctor] containing a provided string. 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 string2buffer = require( '@stdlib/buffer/from-string' ); 41 ``` 42 43 #### string2buffer( str\[, encoding] ) 44 45 Allocates a [buffer][@stdlib/buffer/ctor] containing a provided `string`. 46 47 ```javascript 48 var buf = string2buffer( 'beep boop' ); 49 // returns <Buffer> 50 ``` 51 52 The default character encoding is `utf8`. To specify an alternative encoding, provide an `encoding` argument. 53 54 ```javascript 55 var buf = string2buffer( '7468697320697320612074c3a97374', 'hex' ); 56 // returns <Buffer> 57 ``` 58 59 The following `encodings` are supported: 60 61 - `utf8` (`utf-8`) 62 - `hex` 63 - `binary` 64 - `ascii` 65 - `ucs2` 66 - `base64` 67 68 </section> 69 70 <!-- /.usage --> 71 72 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 73 74 <section class="notes"> 75 76 </section> 77 78 <!-- /.notes --> 79 80 <!-- Package usage examples. --> 81 82 <section class="examples"> 83 84 ## Examples 85 86 <!-- eslint no-undef: "error" --> 87 88 ```javascript 89 var randu = require( '@stdlib/random/base/randu' ); 90 var randint = require( '@stdlib/random/base/discrete-uniform' ); 91 var string2buffer = require( '@stdlib/buffer/from-string' ); 92 93 // Create a buffer from a string: 94 var buf = string2buffer( 'beep boop bop' ); 95 console.log( buf.toString() ); 96 97 // Generate random strings... 98 var i; 99 var j; 100 for ( i = 0; i < 100; i++ ) { 101 j = randint( 0, buf.length ); 102 if ( randu() < 2/buf.length ) { 103 buf[ j ] = 32; // space 104 } else { 105 buf[ j ] = randint( 97, 122 ); 106 } 107 console.log( buf.toString() ); 108 } 109 ``` 110 111 </section> 112 113 <!-- /.examples --> 114 115 <!-- 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. --> 116 117 <section class="references"> 118 119 </section> 120 121 <!-- /.references --> 122 123 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 124 125 <section class="links"> 126 127 [@stdlib/buffer/ctor]: https://www.npmjs.com/package/@stdlib/buffer/tree/main/ctor 128 129 </section> 130 131 <!-- /.links -->