feat(new tool): Middle Endian Converter

Fix #1447
This commit is contained in:
ShareVB committed 2025-08-20 21:26:44 +02:00
1 parent 40901c976d
commit 1127fbd260
4 files changed
+190

No files matched your search

@@ -0,0 +1,13 @@
import { Binary } from '@vicons/tabler';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'Middle-Endian Converter',
path: '/middle-endian-converter',
description: 'Convert bytes from Middle-Endian to Little/Big Endian for given bits precision and type of Middle Endian',
keywords: ['middle', 'endian', 'converter'],
component: () => import('./middle-endian-converter.vue'),
icon: Binary,
createdAt: new Date('2025-08-15'),
category: 'Forensic',
});
@@ -0,0 +1,34 @@
import { describe, expect, it } from 'vitest';
import { convertEndian, formatInteger } from './middle-endian-converter.service';
describe('convertEndian', () => {
it('converts 32-bit byte-swapped correctly', () => {
const result = convertEndian('0A0B0C0D', 32, 'byte-swapped');
expect(result.bigEndian).toBe('0x0B0A0D0C');
expect(result.littleEndian).toBe('0x0C0D0A0B');
});
it('converts 64-bit word-swapped correctly', () => {
const result = convertEndian('0A0B0C0D0E0F1234', 64, 'word-swapped');
expect(result.bigEndian).toBe('0x0C0D0A0B12340E0F');
expect(result.littleEndian).toBe('0x0F0E34120B0A0D0C');
});
it('throws on invalid hex', () => {
expect(() => convertEndian('ZZZZ', 32, 'byte-swapped')).toThrow();
});
it('throws on incorrect length', () => {
expect(() => convertEndian('1234', 32, 'byte-swapped')).toThrow();
});
});
describe('formatInteger', () => {
it('should convert input hex to octal, decimal and hexa', () => {
expect(formatInteger('0xFF')).toEqual({
decimal: '255',
octal: '0o377',
hexadecimal: '0xFF',
});
});
});
@@ -0,0 +1,71 @@
export interface EndianResult {
bigEndian: string
littleEndian: string
};
export function convertEndian(
hexInput: string,
bitLength: 16 | 32 | 64,
middleEndianType: 'byte-swapped' | 'word-swapped',
): EndianResult {
const hex = hexInput.replace(/^0x/, '').toUpperCase();
if (!/^[0-9A-F]+$/.test(hex)) {
throw new Error('Invalid hex input. Only 0-9 and A-F allowed.');
}
const byteCount = bitLength / 8;
if (hex.length !== byteCount * 2) {
throw new Error(`Input must be ${byteCount * 2} hex characters for ${bitLength}-bit.`);
}
const bytes = hex.match(/.{2}/g);
if (!bytes) {
throw new Error('Failed to parse bytes.');
}
const reordered: string[] = [];
if (middleEndianType === 'byte-swapped') {
for (let i = 0; i < bytes.length; i += 2) {
reordered.push(bytes[i + 1], bytes[i]);
}
}
else if (middleEndianType === 'word-swapped') {
for (let i = 0; i < bytes.length; i += 4) {
reordered.push(...bytes.slice(i + 2, i + 4), ...bytes.slice(i, i + 2));
}
}
else {
throw new Error('Unsupported middle-endian format.');
}
return {
bigEndian: `0x${reordered.join('')}`,
littleEndian: `0x${[...reordered].reverse().join('')}`,
};
}
export function formatInteger(hexInput: string): {
decimal: string
octal: string
hexadecimal: string
} {
try {
const hex = hexInput.replace(/^0x/, '');
const intValue = BigInt(`0x${hex}`);
return {
decimal: intValue.toString(10),
octal: `0o${intValue.toString(8)}`,
hexadecimal: `0x${intValue.toString(16).toUpperCase()}`,
};
}
catch {
return {
decimal: 'Invalid',
octal: 'Invalid',
hexadecimal: 'Invalid',
};
}
}
@@ -0,0 +1,72 @@
<script setup lang="ts">
import { convertEndian, formatInteger } from './middle-endian-converter.service';
const inputHex = ref('');
const bitLength = ref(32);
const middleEndianType = ref('byte-swapped');
const result = computed(() => {
try {
const converted = convertEndian(inputHex.value, bitLength.value as never, middleEndianType.value as never);
return {
error: '',
...converted,
...formatInteger(converted.littleEndian),
};
}
catch (e: any) {
return {
error: e.toString(),
bigEndian: '',
littleEndian: '',
octal: '',
decimal: '',
hexadecimal: '',
};
}
});
const bitOptions = [
{ label: '16-bit', value: 16 },
{ label: '32-bit', value: 32 },
{ label: '64-bit', value: 64 },
];
const middleEndianOptions = [
{ label: 'Byte-Swapped', value: 'byte-swapped' },
{ label: 'Word-Swapped', value: 'word-swapped' },
];
</script>
<template>
<n-card title="Endian Converter" style="max-width: 700px; margin: auto;">
<n-space vertical>
<n-input
v-model:value="inputHex"
placeholder="Enter hex (e.g., 3412ABCD)"
clearable
/>
<n-select
v-model:value="bitLength"
:options="bitOptions"
label="Bit Length"
placeholder="Select bit length"
/>
<n-select
v-model:value="middleEndianType"
:options="middleEndianOptions"
label="Middle Endian Type"
placeholder="Select middle-endian format"
/>
<c-alert v-if="inputHex && result.error">
{{ result.error }}
</c-alert>
<n-card v-if="inputHex && !result.error" title="Conversion result">
<input-copyable label="Big Endian:" label-width="120px" label-position="left" :value="result.bigEndian" />
<input-copyable label="Little Endian:" label-width="120px" label-position="left" :value="result.littleEndian" />
<input-copyable label="Decimal:" label-width="120px" label-position="left" :value="result.decimal" mt-2 />
<input-copyable label="Octal:" label-width="120px" label-position="left" :value="result.octal" />
<input-copyable label="Hexadecimal:" label-width="120px" label-position="left" :value="result.hexadecimal" />
</n-card>
</n-space>
</n-card>
</template>