Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/samualtnorman/base64-visualizer. Visualize base64. samual.uk/base64-visualizer
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109import { decodeBase64 } from "@std/encoding"import { createMemo, createSignal, For } from "solid-js"import { t } from "try"
const BASE64 = `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/`
export const App = () => { const [ base64, setBase64 ] = createSignal(``) const cleanBase64 = createMemo(() => base64.$.replace(/[^a-z\d+/]/ig, ``)) const badLength = createMemo(() => (cleanBase64.$.length % 4) == 1)
const decodedBase64Result = createMemo(() => t(() => [ ...decodeBase64(badLength.$ && !/[^a-z\d+/=]/i.test(base64.$) ? cleanBase64.$.slice(0, cleanBase64.$.length - 1) : base64.$) ]))
const base64UnusedBits = createMemo(() => (cleanBase64.$.length * 6) % 8)
const base64LastCharacterValue = createMemo(() => { const lastCharacter = cleanBase64.$.at(-1)
return lastCharacter ? BASE64.indexOf(lastCharacter) : 0 })
const paddingHasValue = createMemo(() => !!(base64LastCharacterValue.$ & ((1 << base64UnusedBits.$) - 1)))
return <> <textarea style={{ "word-break": `break-all`, width: `calc(100% - 8px)` }} spellcheck={false} onInput={({ target }) => { setBase64(target.value.replaceAll(`-`, `+`).replaceAll(`_`, `/`)) }} ></textarea>
{decodedBase64Result.$.ok ? <> {badLength.$ ? <p style={{ color: `red` }}>Warning: Invalid base64 length.</p> : paddingHasValue.$ ? <p style={{ color: `red` }}>Warning: Bad padding.</p> : undefined}
<table> <tbody> <tr> <th style={{ "padding-right": `1rem` }}>Base64</th>
<For each={cleanBase64.$.split(``)}> {character => <td colSpan={6}>{character}</td>} </For> </tr>
<tr> <th style={{ "padding-right": `1rem` }}>Binary</th>
<For each={decodedBase64Result.$.value}> {byte => <> <td>{byte >> 7}</td> <td>{(byte >> 6) & 1}</td> <td>{(byte >> 5) & 1}</td> <td>{(byte >> 4) & 1}</td> <td>{(byte >> 3) & 1}</td> <td>{(byte >> 2) & 1}</td> <td>{(byte >> 1) & 1}</td> <td>{byte & 1}</td> </>} </For>
{base64UnusedBits.$ > 4 && <> <td style={paddingHasValue.$ || badLength.$ ? { color: `red` } : undefined}>{base64LastCharacterValue.$ >> 5}</td> <td style={paddingHasValue.$ || badLength.$ ? { color: `red` } : undefined}>{(base64LastCharacterValue.$ >> 4) & 1}</td> </>}
{base64UnusedBits.$ > 2 && <> <td style={paddingHasValue.$ || badLength.$ ? { color: `red` } : undefined}>{(base64LastCharacterValue.$ >> 3) & 1}</td> <td style={paddingHasValue.$ || badLength.$ ? { color: `red` } : undefined}>{(base64LastCharacterValue.$ >> 2) & 1}</td> </>}
{!!base64UnusedBits.$ && <> <td style={paddingHasValue.$ || badLength.$ ? { color: `red` } : undefined}>{(base64LastCharacterValue.$ >> 1) & 1}</td> <td style={paddingHasValue.$ || badLength.$ ? { color: `red` } : undefined}>{base64LastCharacterValue.$ & 1}</td> </>} </tr>
<tr> <th style={{ "padding-right": `1rem` }}>Bytes</th>
<For each={decodedBase64Result.$.value}> {byte => <td colSpan={8}>{byte}</td>} </For> </tr>
<tr> <th style={{ "padding-right": `1rem` }}>Hex</th>
<For each={decodedBase64Result.$.value}> {byte => <> <td colSpan={4}>{(byte >> 4).toString(16).toUpperCase()}</td> <td colSpan={4}>{(byte & 0xF).toString(16).toUpperCase()}</td> </>} </For> </tr> </tbody> </table> </> : <p style={{ color: `red` }}> {(decodedBase64Result.$.error as any)?.message || String(decodedBase64Result.$.error)} </p>} </>}