tobito
Version:
A package that returns bit representation of simple strings and numbers.
26 lines (22 loc) • 778 B
JavaScript
function tobito(_obj) {
switch(typeof _obj) {
case 'number':
return toBin(_obj)
case 'string':
let myArr = []
for(let char of _obj) {
myArr.push(toBin(char.codePointAt(0).toString()))
}
return myArr
// -1 to denote an unimplemented type
default:
return -1
}
}
// See https://stackoverflow.com/questions/9939760/how-do-i-convert-an-integer-to-binary-in-javascript for more information
// Summary -> Number.toString(2) will also do the job but it hates negative numbers. It is much happier when you
// 'coerce your number to an unsigned integer' first.
function toBin(dec) {
return (dec >>> 0).toString(2)
}
module.exports = tobito