0

Convert binary to decimals

Function JavaScript 1 revisions 111 7 months ago 7 months ago
function binToDec(bin) {
	let num = 0;
	for (let i = 0; i < bin.length; i++) {
		num = num << 1;
		if (bin[i] === '1') ++num;
	}
	return num;
}

This converts a string of a binary number to a decimal, unsigned integer.

binToDec('0010');     // 2
binToDec('10010');    // 18
binToDec('11111111'); // 255