mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-04 05:19:12 -04:00
feat: handle prefix 0x, &H, \x and custom output prefix
This commit is contained in:
parent
73e97f97bb
commit
0c63eb7ca4
1 changed files with 12 additions and 2 deletions
|
@ -8,7 +8,7 @@ const fileName = ref('');
|
||||||
const fileExtension = ref('');
|
const fileExtension = ref('');
|
||||||
const hexInput = ref('');
|
const hexInput = ref('');
|
||||||
const base64Input = computed(() => {
|
const base64Input = computed(() => {
|
||||||
const hexString = hexInput.value?.replace(/[^\da-f]/gi, '');
|
const hexString = hexInput.value?.replace(/^(?:0x|&H|\\x)/gi, '').replace(/[^\da-f]/gi, '');
|
||||||
try {
|
try {
|
||||||
return `data:application/octet-stream;base64,${Buffer.from(hexString, 'hex').toString('base64')}`;
|
return `data:application/octet-stream;base64,${Buffer.from(hexString, 'hex').toString('base64')}`;
|
||||||
}
|
}
|
||||||
|
@ -51,11 +51,13 @@ async function ReadFileAsHex(file: File, separator: string = ' '): Promise<strin
|
||||||
|
|
||||||
const separator = useStorage('hex-converter:sep', ' ');
|
const separator = useStorage('hex-converter:sep', ' ');
|
||||||
const fileInput = ref() as Ref<File>;
|
const fileInput = ref() as Ref<File>;
|
||||||
|
const prefix = useStorage('hex-converter:prefix', '');
|
||||||
const fileHex = computedAsync(async () => {
|
const fileHex = computedAsync(async () => {
|
||||||
const file = fileInput.value;
|
const file = fileInput.value;
|
||||||
const sep = separator.value;
|
const sep = separator.value;
|
||||||
|
const pref = prefix.value;
|
||||||
|
|
||||||
return await ReadFileAsHex(file, sep);
|
return pref + await ReadFileAsHex(file, sep);
|
||||||
});
|
});
|
||||||
const { copy: copyFileHex } = useCopy({ source: fileHex, text: 'Hex string copied to the clipboard' });
|
const { copy: copyFileHex } = useCopy({ source: fileHex, text: 'Hex string copied to the clipboard' });
|
||||||
|
|
||||||
|
@ -119,6 +121,14 @@ function onUpload(file: File) {
|
||||||
mb-2
|
mb-2
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<c-input-text
|
||||||
|
v-model:value="prefix"
|
||||||
|
label="Prefix"
|
||||||
|
label-position="left"
|
||||||
|
placeholder="Enter a prefix (ie, 0x, &H or empty)"
|
||||||
|
mb-2
|
||||||
|
/>
|
||||||
|
|
||||||
<n-divider />
|
<n-divider />
|
||||||
|
|
||||||
<n-form-item label="File in Hex">
|
<n-form-item label="File in Hex">
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue