README.md (2448B)
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 # From Binary String 22 23 > Create an unsigned 32-bit integer from a [literal bit representation][@stdlib/number/uint32/base/to-binary-string]. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var fromBinaryStringUint32 = require( '@stdlib/number/uint32/base/from-binary-string' ); 31 ``` 32 33 #### fromBinaryStringUint32( bstr ) 34 35 Creates an unsigned 32-bit integer from a [literal bit representation][@stdlib/number/uint32/base/to-binary-string]. 36 37 ```javascript 38 var bstr = '01010101010101010101010101010101'; 39 var val = fromBinaryStringUint32( bstr ); 40 // returns 1431655765 41 42 bstr = '00000000000000000000000000000000'; 43 val = fromBinaryStringUint32( bstr ); 44 // returns 0 45 46 bstr = '00000000000000000000000000000010'; 47 val = fromBinaryStringUint32( bstr ); 48 // returns 2 49 50 bstr = '11111111111111111111111111111111'; 51 val = fromBinaryStringUint32( bstr ); 52 // returns 4294967295 53 ``` 54 55 </section> 56 57 <!-- /.usage --> 58 59 <section class="examples"> 60 61 ## Examples 62 63 <!-- eslint no-undef: "error" --> 64 65 ```javascript 66 var randu = require( '@stdlib/random/base/randu' ); 67 var round = require( '@stdlib/math/base/special/round' ); 68 var MAX_UINT = require( '@stdlib/constants/uint32/max' ); 69 var toBinaryStringUint32 = require( '@stdlib/number/uint32/base/to-binary-string' ); 70 var fromBinaryStringUint32 = require( '@stdlib/number/uint32/base/from-binary-string' ); 71 72 var b; 73 var x; 74 var y; 75 var i; 76 77 // Convert random integers to literal bit representations and then convert them back... 78 for ( i = 0; i < 100; i++ ) { 79 x = round( randu()*MAX_UINT ); 80 b = toBinaryStringUint32( x ); 81 y = fromBinaryStringUint32( b ); 82 console.log( '%d => %s => %d', x, b, y ); 83 } 84 ``` 85 86 </section> 87 88 <!-- /.examples --> 89 90 <section class="links"> 91 92 [@stdlib/number/uint32/base/to-binary-string]: https://www.npmjs.com/package/@stdlib/number/tree/main/uint32/base/to-binary-string 93 94 </section> 95 96 <!-- /.links -->