Public
Edited
Jun 17, 2023
Insert cell
Insert cell
Insert cell
parseInt("010101", 2).toString(16)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
result = (from, fromBase, toBase) => {
var scrollable = d3.create("div")
.style("overflow-x", "scroll")
var input = formattedFrom(from, toBase)

for (var i in input) {
if (toDecimal(input[i]) >= fromBase) {
scrollable
.append("p")
.style("color", "#b3261e")
.text(`"${input[i]}" não existe na base ${fromBase}`)
return scrollable.node()
}
}

var result = scrollable
.append("p")
.style("text-align", "center")
result
.append("span")
.text("(")

result
.selectAll(".from")
.data(chunckedFrom(from, toBase))
.enter()
.append("span")
.style("color", (_, i) => i % 2 == 0 ? "#b3261e" : "#085786")
.text(d => d.join(""))

result
.append("span")
.html(`)<sub>${fromBase}</sub> = (`)

result
.selectAll(".to")
.data(chunckedTo(from, fromBase, toBase).map(fromDecimal))
.enter()
.append("span")
.style("color", (_, i) => i % 2 == 0 ? "#b3261e" : "#085786")
.text(d => d.join(""))

result
.append("span")
.html(`)<sub>${toBase}</sub>`)

var top = chunckedFrom(from, toBase), down = chunckedTo(from, fromBase, toBase)
if (chunkSize(toBase) < chunkSize(fromBase)) {
var temp = top
top = down
down = temp
}

var table = scrollable.append("table")
function append(chunks) {
var tr = table.append("tr")
for (var c in chunks) {
var colspan = top[0].length / chunks[c].length
tr.selectAll(`.td${c}`)
.data(chunks[c])
.enter()
.append("td")
.attr("colspan", colspan)
.style("color", c % 2 == 0 ? "#b3261e" : "#085786")
.style("font-weight", (_, i) => top[c][i] == 1 || colspan > 1 ? "900" : "normal")
.html(d => d)
}
}
append(top)
append(indexes(0, down.length).map(_ => chunkConverter(fromBase, toBase).map(x => `2<sup>${Math.log2(x)}</sup>`)))
append(indexes(0, down.length).map(_ => chunkConverter(fromBase, toBase)))
append(down.map(toDecimal))

if (fromBase <= 10 && toBase <= 10) return scrollable.node()
var details = scrollable.append("details")

details
.append("summary")
.text("Ver tabela de conversão")

table = details.append("table")

var tr = table.append("tr"), b = fromBase > 10 ? fromBase : toBase

tr.append("th")
.text(`Base ${10}`)
tr.append("th")
.text(`Base ${b}`)
for (var i = 0; i < b - 10; i++) {
tr = table.append("tr")
tr.append("td")
.text(`${10 + i}`)
tr.append("td")
.text(`${alphabet[i]}`)
}
return scrollable.node()
}
Insert cell
html`
<style type="text/css">
td, th {
border: 1px solid black;
width: 300px;
text-align: center;
}
</style>
`
Insert cell
formattedTo = (from, toBase) => chunckedTo(from, toBase).join("")
Insert cell
chunckedTo = (from, fromBase, toBase) => chunckedFrom(from, toBase).map(c => convertChunk(c, chunkConverter(fromBase, toBase)))
Insert cell
chunckedFrom = (from, toBase) => chunks([...formattedFrom(from, toBase)].reverse(), chunkSize(toBase)).map(c => c.reverse()).reverse()
Insert cell
formattedFrom = (from, toBase) => from.padStart(chunkSize(toBase) * chunkAmount(from, toBase), "0")
Insert cell
chunkConverter = (fromBase, toBase) => indexes(0, chunkSize(fromBase) * chunkSize(toBase)).reverse().map(i => 2 ** i)
Insert cell
chunkAmount = (from, base) => Math.ceil(from.length / chunkSize(base))
Insert cell
chunkSize = (base) => Math.log2(base)
Insert cell
convertChunk = (from, converter) => from.length < converter.length ? converter.reduce((a, b) => a[0] + b <= toDecimal(from) ? (a[0] += b, a[1].push(1), a) : (a[1].push(0), a), [0, []])[1] : [from.reduce((a, f, i) => f == 1 ? a += converter[i] : a, 0)]
Insert cell
toDecimal = (d) => isNaN(d) ? [alphabet.indexOf(d) + 10] : d
Insert cell
fromDecimal = (d) => d >= 10 ? [alphabet[d - 10]] : d
Insert cell
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Insert cell
chunks = (arr, size) => indexes(0, Math.ceil(arr.length / size)).map((_, i) => i * size).map(start => arr.slice(start, start + size))
Insert cell
indexes = (start, end) => Array.apply(null, {length: end}).map(Number.call, Number).slice(start)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more