10 examples of 'js number to binary' in JavaScript

Every line of 'js number to binary' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
21function binaryToByte(bin) {
22 return parseInt(bin, 2);
23}
103function binaryToDecimal(data) {
104 var ret = "";
105 while (data !== "0") {
106 var end = 0;
107 var fullName = "";
108 var i = 0;
109 for (; i < data.length; i++) {
110 end = 2 * end + parseInt(data[i], 10);
111 if (end >= 10) {
112 fullName += "1";
113 end -= 10;
114 } else {
115 fullName += "0";
116 }
117 }
118 ret = end.toString() + ret;
119 data = fullName.slice(fullName.indexOf("1"));
120 }
121 return ret;
122}
260function convertToBinaryString(decimal_value, digits) {
261 binary_value = Number(decimal_value).toString(2);
262 padded_string = "0".repeat(digits)
263 return padded_string.substr(binary_value.length) + binary_value
264};
61static decToBinary(dec) {
62 return (dec >>> 0).toString(2);
63 }
62unsignedNumberFromBinary(data) {
63 //little adian
64 let val = data[data.length - 1];
65 for (let i = data.length - 2; i >= 0; i--) {
66 val = val * 256 + data[i];
67 }
68 return val;
69}
3export function encodeNumberAsJSNumber(s: number | string, isWrapped: boolean = false): string {
4 return bignum(s, "number", isWrapped);
5}
102function bin(value) {
103 return typeof value === 'string' ? parseInt(value, 2) : value;
104}
54function fromValueToBinary(value, convertFrom) {
55 var data;
56 if (data = bytes[convertFrom]) {
57 data.type[0] = value;
58 return byte.slice(0, data.size)
59 } else {
60 throw new Error("Invalid convert string")
61 }
62}
24function binary(value, padding) {
25 const bignum = new BigNum(value, 10).toString(2);
26 return padding !== false && bignum.length < 64
27 ? CACHE_64_BIT_ZEROS.substr(0, 64 - bignum.length) + bignum
28 : bignum;
29}
23function dec2bin(input, places) {
24
25 // exit if input is an error
26 if (input instanceof Error) {
27 return number;
28 }
29
30 // cast input to number
31 var number = parseInt(input);
32
33 if (!/^-?[0-9]{1,3}$/.test(number) || (0, _isnan2.default)(number)) {
34 return _error2.default.value;
35 }
36
37 // Return error.if number is not decimal, is lower than -512, or is greater than 511
38 if (number < -512 || number > 511) {
39 return _error2.default.num;
40 }
41
42 // Ignore places and return a 10-character binary number if number is negative
43 if (number < 0) {
44 return '1' + (0, _rept2.default)('0', 9 - (512 + number).toString(2).length) + (512 + number).toString(2);
45 }
46
47 // Convert decimal number to binary
48 var result = parseInt(number, 10).toString(2);
49
50 // Return binary number using the minimum number of characters necessary if places is undefined
51 if (typeof places === 'undefined') {
52 return result;
53 } else {
54 // Return error.if places is nonnumeric
55 if (!/^-?[0-9]{1,3}$/.test(places) || (0, _isnan2.default)(places)) {
56 return _error2.default.value;
57 }
58
59 // Return error.if places is negative
60 if (places < 0) {
61 return _error2.default.num;
62 }
63
64 // Truncate places in case it is not an integer
65 places = Math.floor(places);
66
67 // Pad return value with leading 0s (zeros) if necessary (using Underscore.string)
68 return places >= result.length ? (0, _rept2.default)('0', places - result.length) + result : _error2.default.num;
69 }
70} // Copyright 2015 JC Fisher

Related snippets