145 lines
5.8 KiB
Vue
145 lines
5.8 KiB
Vue
<script setup lang="ts">
|
|
// The converter: write text, hex, binary, decimal, octal or Base64 and read
|
|
// it in every other form at once, byte by byte (or word by word) below.
|
|
import {
|
|
charLabel,
|
|
ENCODING_NAMES,
|
|
ENCODINGS,
|
|
FORMAT_NAMES,
|
|
FORMATS,
|
|
formatAs,
|
|
parseAs,
|
|
signed,
|
|
toWords,
|
|
type ConvertOptions,
|
|
type Format,
|
|
type WordBits,
|
|
} from '~/lib/convert'
|
|
|
|
const ed = useEditor()
|
|
const close = () => (ed.dialog.value = null)
|
|
|
|
const KEY = 'logigate:convert'
|
|
function load(): { input: string; from: Format; opts: ConvertOptions } {
|
|
try {
|
|
const s = JSON.parse(localStorage.getItem(KEY) ?? '')
|
|
if (s && typeof s.input === 'string' && FORMATS.includes(s.from)) return s
|
|
} catch {}
|
|
return { input: 'Hello!', from: 'text', opts: { enc: 'utf8', word: 8, endian: 'big' } }
|
|
}
|
|
const start = load()
|
|
const input = ref(start.input)
|
|
const from = ref<Format>(start.from)
|
|
const opts = reactive<ConvertOptions>({ ...start.opts })
|
|
watch([input, from, opts], () => {
|
|
try {
|
|
localStorage.setItem(KEY, JSON.stringify({ input: input.value, from: from.value, opts }))
|
|
} catch {}
|
|
})
|
|
|
|
const bytes = computed(() => parseAs(input.value, from.value, opts))
|
|
const outputs = computed(() =>
|
|
FORMATS.filter(f => f !== from.value).map(f => {
|
|
const b = bytes.value
|
|
return { f, res: b.ok ? formatAs(b.value, f, opts) : null }
|
|
}),
|
|
)
|
|
const words = computed(() => (bytes.value.ok ? toWords(bytes.value.value, opts) : []))
|
|
const ROWS = 256
|
|
/** A word as a character, where a word is one character of the encoding (a UTF-8 byte above 7F is only part of one). */
|
|
const charOf = (v: number) => {
|
|
const { enc, word, endian } = opts
|
|
if (word === 8 && (enc === 'ascii' || enc === 'latin1' || (enc === 'utf8' && v < 0x80))) return charLabel(v)
|
|
if (word === 16 && (enc === 'utf16le' ? endian === 'little' : enc === 'utf16be' && endian === 'big')) return charLabel(v)
|
|
if (word === 32 && enc === 'utf32be' && endian === 'big') return charLabel(v)
|
|
return '·'
|
|
}
|
|
|
|
/** Takes an output as the new input. */
|
|
function use(f: Format, text: string) {
|
|
from.value = f
|
|
input.value = text
|
|
}
|
|
const WORDS: WordBits[] = [8, 16, 32]
|
|
</script>
|
|
|
|
<template>
|
|
<div class="modal-backdrop" @click.self="close">
|
|
<div class="modal convert" role="dialog" aria-labelledby="convert-title">
|
|
<div class="flex items-start justify-between gap-4">
|
|
<h2 id="convert-title">Converter</h2>
|
|
<button class="tb-btn" title="Close" @click="close"><Icon name="lucide:x" class="size-4" /></button>
|
|
</div>
|
|
<p class="hint">Write in one form, read it in all others. Hex and binary take 0x and 0b, spaces and commas, or one long run of digits.</p>
|
|
|
|
<div class="convert-opts">
|
|
<label>
|
|
<span>Text encoding</span>
|
|
<select v-model="opts.enc" class="tb-select">
|
|
<option v-for="e in ENCODINGS" :key="e" :value="e">{{ ENCODING_NAMES[e] }}</option>
|
|
</select>
|
|
</label>
|
|
<label>
|
|
<span>Word</span>
|
|
<div class="seg">
|
|
<button v-for="w in WORDS" :key="w" :class="{ active: opts.word === w }" @click="opts.word = w">{{ w }} bits</button>
|
|
</div>
|
|
</label>
|
|
<label v-if="opts.word > 8">
|
|
<span>Byte order</span>
|
|
<div class="seg">
|
|
<button :class="{ active: opts.endian === 'big' }" title="Highest byte first" @click="opts.endian = 'big'">Big-endian</button>
|
|
<button :class="{ active: opts.endian === 'little' }" title="Lowest byte first" @click="opts.endian = 'little'">Little-endian</button>
|
|
</div>
|
|
</label>
|
|
</div>
|
|
|
|
<div class="convert-in">
|
|
<div class="seg">
|
|
<button v-for="f in FORMATS" :key="f" :class="{ active: from === f }" @click="from = f">{{ FORMAT_NAMES[f] }}</button>
|
|
</div>
|
|
<textarea v-model="input" class="field-input convert-text" rows="3" spellcheck="false" :placeholder="`${FORMAT_NAMES[from]} here`" />
|
|
<p v-if="!bytes.ok" class="convert-error">{{ bytes.error }}</p>
|
|
<p v-else class="hint">{{ bytes.value.length }} bytes · {{ words.length }} {{ opts.word }}-bit words</p>
|
|
</div>
|
|
|
|
<div class="convert-outs">
|
|
<div v-for="o in outputs" :key="o.f" class="convert-out">
|
|
<div class="convert-out-head">
|
|
<span>{{ FORMAT_NAMES[o.f] }}</span>
|
|
<span v-if="o.res?.ok && o.res.note" class="hint">{{ o.res.note }}</span>
|
|
<span class="grow" />
|
|
<template v-if="o.res?.ok">
|
|
<button class="icon-btn" title="Copy" @click="ed.copyText(o.res.value, `${FORMAT_NAMES[o.f]} copied`)"><Icon name="lucide:copy" class="size-3.5" /></button>
|
|
<button class="icon-btn" title="Write in this form from here on" @click="use(o.f, o.res.value)"><Icon name="lucide:arrow-up" class="size-3.5" /></button>
|
|
</template>
|
|
</div>
|
|
<pre class="convert-value">{{ o.res ? (o.res.ok ? o.res.value : o.res.error) : '' }}</pre>
|
|
</div>
|
|
</div>
|
|
|
|
<template v-if="words.length">
|
|
<h3>Word by word</h3>
|
|
<div class="convert-table-wrap">
|
|
<table class="convert-table tabular-nums">
|
|
<thead>
|
|
<tr><th>#</th><th>Hex</th><th>Binary</th><th>Decimal</th><th>Signed</th><th>Character</th></tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr v-for="(v, i) in words.slice(0, ROWS)" :key="i">
|
|
<td>{{ i }}</td>
|
|
<td>{{ v.toString(16).toUpperCase().padStart(opts.word / 4, '0') }}</td>
|
|
<td>{{ v.toString(2).padStart(opts.word, '0') }}</td>
|
|
<td>{{ v }}</td>
|
|
<td>{{ signed(v, opts.word) }}</td>
|
|
<td>{{ charOf(v) }}</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
<p v-if="words.length > ROWS" class="hint">and {{ words.length - ROWS }} more</p>
|
|
</div>
|
|
</template>
|
|
</div>
|
|
</div>
|
|
</template>
|