Compare commits

..
Author SHA1 Message Date
sharevb 71faa2bb0c feat(new tool): Hex Converter
Convert hex buffer to (un)signed integer/float
Fix #1447
2025-03-10 21:28:26 +01:00
12 changed files with 2370 additions and 2342 deletions

No files matched your search

+9 -6
View File
@@ -64,7 +64,6 @@ declare module '@vue/runtime-core' {
'CTextCopyable.demo': typeof import('./src/ui/c-text-copyable/c-text-copyable.demo.vue')['default']
CTooltip: typeof import('./src/ui/c-tooltip/c-tooltip.vue')['default']
'CTooltip.demo': typeof import('./src/ui/c-tooltip/c-tooltip.demo.vue')['default']
CurrencyConverter: typeof import('./src/tools/currency-converter/currency-converter.vue')['default']
DateTimeConverter: typeof import('./src/tools/date-time-converter/date-time-converter.vue')['default']
'DemoHome.page': typeof import('./src/ui/demo/demo-home.page.vue')['default']
DemoWrapper: typeof import('./src/ui/demo/demo-wrapper.vue')['default']
@@ -84,6 +83,7 @@ declare module '@vue/runtime-core' {
GitMemo: typeof import('./src/tools/git-memo/git-memo.vue')['default']
'GitMemo.content': typeof import('./src/tools/git-memo/git-memo.content.md')['default']
HashText: typeof import('./src/tools/hash-text/hash-text.vue')['default']
HexConverter: typeof import('./src/tools/hex-converter/hex-converter.vue')['default']
HmacGenerator: typeof import('./src/tools/hmac-generator/hmac-generator.vue')['default']
'Home.page': typeof import('./src/pages/Home.page.vue')['default']
HtmlEntities: typeof import('./src/tools/html-entities/html-entities.vue')['default']
@@ -131,22 +131,25 @@ declare module '@vue/runtime-core' {
MetaTagGenerator: typeof import('./src/tools/meta-tag-generator/meta-tag-generator.vue')['default']
MimeTypes: typeof import('./src/tools/mime-types/mime-types.vue')['default']
NavbarButtons: typeof import('./src/components/NavbarButtons.vue')['default']
NCheckbox: typeof import('naive-ui')['NCheckbox']
NCode: typeof import('naive-ui')['NCode']
NCollapseTransition: typeof import('naive-ui')['NCollapseTransition']
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
NDatePicker: typeof import('naive-ui')['NDatePicker']
NDivider: typeof import('naive-ui')['NDivider']
NDynamicInput: typeof import('naive-ui')['NDynamicInput']
NEllipsis: typeof import('naive-ui')['NEllipsis']
NFormItem: typeof import('naive-ui')['NFormItem']
NH1: typeof import('naive-ui')['NH1']
NH3: typeof import('naive-ui')['NH3']
NIcon: typeof import('naive-ui')['NIcon']
NInputNumber: typeof import('naive-ui')['NInputNumber']
NLayout: typeof import('naive-ui')['NLayout']
NLayoutSider: typeof import('naive-ui')['NLayoutSider']
NMenu: typeof import('naive-ui')['NMenu']
NP: typeof import('naive-ui')['NP']
NSelect: typeof import('naive-ui')['NSelect']
NTable: typeof import('naive-ui')['NTable']
NRadio: typeof import('naive-ui')['NRadio']
NRadioGroup: typeof import('naive-ui')['NRadioGroup']
NScrollbar: typeof import('naive-ui')['NScrollbar']
NSpace: typeof import('naive-ui')['NSpace']
NSwitch: typeof import('naive-ui')['NSwitch']
NumeronymGenerator: typeof import('./src/tools/numeronym-generator/numeronym-generator.vue')['default']
OtpCodeGeneratorAndValidator: typeof import('./src/tools/otp-code-generator-and-validator/otp-code-generator-and-validator.vue')['default']
PasswordStrengthAnalyser: typeof import('./src/tools/password-strength-analyser/password-strength-analyser.vue')['default']
+3 -2
View File
@@ -53,6 +53,7 @@
"@vueuse/head": "^1.0.0",
"@vueuse/router": "^10.0.0",
"bcryptjs": "^2.4.3",
"byte-data": "^19.0.1",
"change-case": "^4.1.2",
"colord": "^2.9.3",
"composerize-ts": "^0.6.2",
@@ -60,8 +61,6 @@
"cron-validator": "^1.3.1",
"cronstrue": "^2.26.0",
"crypto-js": "^4.1.1",
"currency-codes-ts": "^3.0.0",
"currency-exchanger-js": "^1.0.4",
"date-fns": "^2.29.3",
"dompurify": "^3.0.6",
"email-normalizer": "^1.0.0",
@@ -69,6 +68,7 @@
"figlet": "^1.7.0",
"figue": "^1.2.0",
"fuse.js": "^6.6.2",
"hex-array": "^1.0.0",
"highlight.js": "^11.7.0",
"iarna-toml-esm": "^3.0.5",
"ibantools": "^4.3.3",
@@ -117,6 +117,7 @@
"@types/bcryptjs": "^2.4.2",
"@types/crypto-js": "^4.1.1",
"@types/dompurify": "^3.0.5",
"@types/hex-array": "^1.0.2",
"@types/jsdom": "^21.0.0",
"@types/lodash": "^4.14.192",
"@types/mime-types": "^2.1.1",
+1598 -2196
View File
File diff suppressed because it is too large. Load diff
@@ -1,119 +0,0 @@
<script setup lang="ts">
import { code, countries, country } from 'currency-codes-ts';
import converter from 'currency-exchanger-js';
import moneysData from './moneys.json';
import { useQueryParamOrStorage } from '@/composable/queryParams';
const allCurrencies = Object.entries(moneysData).map(([k, v]) => ({ value: k, label: v || k }));
const otherCurrencies = useQueryParamOrStorage<{ name: string }[]>({ name: 'to', storageName: 'currency-conv:others', defaultValue: [{ name: 'usd' }] });
const currentCurrency = useQueryParamOrStorage<string>({ name: 'from', storageName: 'currency-conv:cur', defaultValue: 'eur' });
const amount = ref(1);
const currentDatetime = ref(Date.now());
const convertedCurrencies = computedAsync<Record<string, number>>(async () => {
const currentCurrencyValue = currentCurrency.value;
const currentDatetimeValue = currentDatetime.value;
const amountValue = amount.value;
const otherCurrenciesValues = otherCurrencies.value;
let result = {};
for (const targetCurrency of otherCurrenciesValues) {
const value = await converter.convertOnDate(amountValue, currentCurrencyValue, targetCurrency.name, new Date(currentDatetimeValue));
result = { ...result, [targetCurrency.name]: value };
}
return result;
});
const countryToCurrenciesInput = ref('France');
const allCountries = countries();
const countryToCurrenciesOutput = computed(() => country(countryToCurrenciesInput.value));
const currencyToCountriesInput = ref('eur');
const currencyToCountriesOutput = computed(() => code(currencyToCountriesInput.value));
</script>
<template>
<div>
<c-card title="Currency Converter" mb-2>
<c-select
v-model:value="currentCurrency"
label="From"
label-position="left"
searchable
:options="allCurrencies"
mb-2
/>
<n-form-item label="Amount:" label-placement="left" mb-2>
<n-input-number v-model:value="amount" :min="0" />
</n-form-item>
<n-form-item label="For Date:" label-placement="left" mb-2>
<n-date-picker
v-model:value="currentDatetime"
type="date"
/>
</n-form-item>
<c-card title="Converted currencies">
<n-dynamic-input
v-model:value="otherCurrencies"
show-sort-button
:on-create="() => ({ name: 'eur' })"
>
<template #default="{ value }">
<div flex flex-wrap items-center gap-1>
<n-select
v-model:value="value.name"
filterable
placeholder="Please select a currency"
:options="allCurrencies"
w-full
/>
<input-copyable readonly :value="convertedCurrencies ? convertedCurrencies[value.name] : 0" />
</div>
</template>
</n-dynamic-input>
</c-card>
</c-card>
<c-card title="Country to Currencies" mb-2>
<c-select
v-model:value="countryToCurrenciesInput"
label="Country"
label-position="left"
searchable
:options="allCountries"
/>
<n-divider />
<ul>
<li v-for="(currency, ix) in countryToCurrenciesOutput" :key="ix">
{{ currency.currency }} [{{ currency.code }}/{{ currency.number }} - {{ currency.digits }}digits] (also in: {{ currency.countries?.join(', ') }})
</li>
</ul>
</c-card>
<c-card title="Currencies to Countries" mb-2>
<c-select
v-model:value="currencyToCountriesInput"
label="Currency"
label-position="left"
searchable
:options="allCurrencies"
/>
<n-divider />
<n-p v-if="currencyToCountriesOutput">
{{ currencyToCountriesOutput.currency }} [{{ currencyToCountriesOutput.code }}/{{ currencyToCountriesOutput.number }} - {{ currencyToCountriesOutput.digits }}digits]
</n-p>
<ul v-if="currencyToCountriesOutput">
<li v-for="(countryName, ix) in currencyToCountriesOutput.countries" :key="ix">
{{ countryName }}
</li>
</ul>
</c-card>
</div>
</template>
@@ -1,4 +0,0 @@
declare module 'currency-exchanger-js'{
export function convertOnDate(value: number,fromCurrency: string,toCurrency: string,inputDate: Date): number;
export function convert(value: number,fromCurrency: string,toCurrency: string): number;
}
-12
View File
@@ -1,12 +0,0 @@
import { Currency } from '@vicons/tabler';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'Currency Converter',
path: '/currency-converter',
description: 'Convert currency values using https://github.com/fawazahmed0/exchange-api',
keywords: ['currency', 'converter'],
component: () => import('./currency-converter.vue'),
icon: Currency,
createdAt: new Date('2024-08-15'),
});
-1
View File
@@ -1 +0,0 @@
{ "1000sats": "", "1inch": "1inch", "aave": "Aave", "ada": "Cardano", "aed": "Emirati Dirham", "afn": "Afghan Afghani", "agix": "SingularityNET", "akt": "Akash Network", "algo": "Algorand", "all": "Albanian Lek", "amd": "Armenian Dram", "amp": "Amp", "ang": "Dutch Guilder", "aoa": "Angolan Kwanza", "ape": "ApeCoin", "apt": "Aptos", "ar": "Arweave", "arb": "Arbitrum", "ars": "Argentine Peso", "atom": "Cosmos", "ats": "Austrian Schilling", "aud": "Australian Dollar", "avax": "Avalanche", "awg": "Aruban or Dutch Guilder", "axs": "Axie Infinity", "azm": "Azerbaijani Manat", "azn": "Azerbaijan Manat", "bake": "BakeryToken", "bam": "Bosnian Convertible Mark", "bat": "Basic Attention Token", "bbd": "Barbadian or Bajan Dollar", "bch": "Bitcoin Cash", "bdt": "Bangladeshi Taka", "bef": "Belgian Franc", "bgn": "Bulgarian Lev", "bhd": "Bahraini Dinar", "bif": "Burundian Franc", "bmd": "Bermudian Dollar", "bnb": "Binance Coin", "bnd": "Bruneian Dollar", "bob": "Bolivian Bolíviano", "brl": "Brazilian Real", "bsd": "Bahamian Dollar", "bsv": "Bitcoin SV", "bsw": "Biswap", "btc": "Bitcoin", "btcb": "Bitcoin BEP2", "btg": "Bitcoin Gold", "btn": "Bhutanese Ngultrum", "btt": "BitTorrent", "busd": "Binance USD", "bwp": "Botswana Pula", "byn": "Belarusian Ruble", "byr": "Belarusian Ruble", "bzd": "Belizean Dollar", "cad": "Canadian Dollar", "cake": "PancakeSwap", "cdf": "Congolese Franc", "celo": "Celo", "cfx": "Conflux", "chf": "Swiss Franc", "chz": "Chiliz", "clp": "Chilean Peso", "cnh": "Chinese Yuan Renminbi Offshore", "cny": "Chinese Yuan Renminbi", "comp": "Compound", "cop": "Colombian Peso", "crc": "Costa Rican Colon", "cro": "Crypto.com Chain", "crv": "Curve DAO Token", "cspr": "Casper", "cuc": "Cuban Convertible Peso", "cup": "Cuban Peso", "cve": "Cape Verdean Escudo", "cvx": "Convex Finance", "cyp": "Cypriot Pound", "czk": "Czech Koruna", "dai": "DAI", "dash": "Digital Cash", "dcr": "Decred", "dem": "German Deutsche Mark", "dfi": "DfiStarter", "djf": "Djiboutian Franc", "dkk": "Danish Krone", "doge": "Dogecoin", "dop": "Dominican Peso", "dot": "Polkadot", "dydx": "dYdX", "dzd": "Algerian Dinar", "eek": "Estonian Kroon", "egld": "Elrond", "egp": "Egyptian Pound", "enj": "Enjin Coin", "eos": "EOS", "ern": "Eritrean Nakfa", "esp": "Spanish Peseta", "etb": "Ethiopian Birr", "etc": "Ethereum Classic", "eth": "Ethereum", "eur": "Euro", "fei": "Fei USD", "fil": "Filecoin", "fim": "Finnish Markka", "fjd": "Fijian Dollar", "fkp": "Falkland Island Pound", "flow": "Flow", "flr": "FLARE", "frax": "Frax", "frf": "French Franc", "ftm": "Fantom", "ftt": "FarmaTrust", "fxs": "Frax Share", "gala": "Gala", "gbp": "British Pound", "gel": "Georgian Lari", "ggp": "Guernsey Pound", "ghc": "Ghanaian Cedi", "ghs": "Ghanaian Cedi", "gip": "Gibraltar Pound", "gmd": "Gambian Dalasi", "gmx": "Goldmaxcoin", "gnf": "Guinean Franc", "gno": "Gnosis", "grd": "Greek Drachma", "grt": "The Graph", "gt": "GateToken", "gtq": "Guatemalan Quetzal", "gusd": "Gemini US Dollar", "gyd": "Guyanese Dollar", "hbar": "Hedera", "hkd": "Hong Kong Dollar", "hnl": "Honduran Lempira", "hnt": "Helium", "hot": "Hydro Protocol", "hrk": "Croatian Kuna", "ht": "Huobi Token", "htg": "Haitian Gourde", "huf": "Hungarian Forint", "icp": "Internet Computer", "idr": "Indonesian Rupiah", "iep": "Irish Pound", "ils": "Israeli Shekel", "imp": "Isle of Man Pound", "imx": "Immutable X", "inj": "Injective", "inr": "Indian Rupee", "iqd": "Iraqi Dinar", "irr": "Iranian Rial", "isk": "Icelandic Krona", "itl": "Italian Lira", "jep": "Jersey Pound", "jmd": "Jamaican Dollar", "jod": "Jordanian Dinar", "jpy": "Japanese Yen", "kas": "", "kava": "Kava", "kcs": "Kucoin", "kda": "Kadena", "kes": "Kenyan Shilling", "kgs": "Kyrgyzstani Som", "khr": "Cambodian Riel", "klay": "Klaytn", "kmf": "Comorian Franc", "knc": "Kyber Network Crystals", "kpw": "North Korean Won", "krw": "South Korean Won", "ksm": "Kusama", "kwd": "Kuwaiti Dinar", "kyd": "Caymanian Dollar", "kzt": "Kazakhstani Tenge", "lak": "Lao Kip", "lbp": "Lebanese Pound", "ldo": "Lido DAO Token", "leo": "LEOcoin", "link": "Chainlink", "lkr": "Sri Lankan Rupee", "lrc": "Loopring", "lrd": "Liberian Dollar", "lsl": "Basotho Loti", "ltc": "Litecoin", "ltl": "Lithuanian Litas", "luf": "Luxembourg Franc", "luna": "Terra", "lunc": "", "lvl": "Latvian Lat", "lyd": "Libyan Dinar", "mad": "Moroccan Dirham", "mana": "Mana Coin Decentraland", "matic": "Polygon", "mbx": "MobieCoin", "mdl": "Moldovan Leu", "mga": "Malagasy Ariary", "mgf": "Malagasy Franc", "mina": "Mina", "mkd": "Macedonian Denar", "mkr": "Maker", "mmk": "Burmese Kyat", "mnt": "Mongolian Tughrik", "mop": "Macau Pataca", "mro": "Mauritanian Ouguiya", "mru": "Mauritanian Ouguiya", "mtl": "Maltese Lira", "mur": "Mauritian Rupee", "mvr": "Maldivian Rufiyaa", "mwk": "Malawian Kwacha", "mxn": "Mexican Peso", "mxv": "", "myr": "Malaysian Ringgit", "mzm": "Mozambican Metical", "mzn": "Mozambican Metical", "nad": "Namibian Dollar", "near": "NEAR Protocol", "neo": "NEO", "nexo": "NEXO", "nft": "NFLine truncated
@@ -0,0 +1,250 @@
import { describe, expect, it } from 'vitest';
import { cleanHex, decodeNumber, decodeStruct, encodeStruct, getCoderFromTypeName, parseNumber } from './hex-converter.service';
describe('cleanHex', () => {
it('should remove 0x and \\x prefixes from a hex string', () => {
expect(cleanHex('0x1234')).toBe('1234');
expect(cleanHex('\\x1234')).toBe('1234');
expect(cleanHex('1234')).toBe('1234');
});
});
describe('decodeNumber', () => {
it('should convert a number to binary', () => {
expect(decodeNumber(10, 8, 'bin')).toBe('00001010');
});
it('should convert a number to hex', () => {
expect(decodeNumber(10, 8, 'hex')).toBe('0a');
});
it('should convert a number to character', () => {
expect(decodeNumber(65, 8, 'char')).toBe('A');
});
it('should return the number itself for decimal conversion', () => {
expect(decodeNumber(10, 8, 'dec')).toBe(10);
});
});
describe('parseNumber', () => {
it('should parse hexadecimal input correctly', () => {
expect(parseNumber('0x1F')).toBe(31);
});
it('should parse binary input correctly', () => {
expect(parseNumber('0b1101')).toBe(13);
});
it('should parse string input correctly into code points', () => {
expect(parseNumber('abc')).toEqual([97, 98, 99]);
});
it('should return number when input is already a number', () => {
expect(parseNumber(42)).toBe(42);
});
it('should return 0 for falsy input', () => {
expect(parseNumber('')).toBe(0);
});
});
describe('getCoderFromTypeName', () => {
it('should return correct coder options for a basic integer type', () => {
const result = getCoderFromTypeName('uint16');
expect(result.type.bits).toBe(16);
expect(result.type.signed).toBe(false);
expect(result.size).toBe(1);
});
it('should handle big-endian types correctly', () => {
const result = getCoderFromTypeName('int32be');
expect(result.type.bits).toBe(32);
expect(result.type.be).toBe(true);
});
it('should return correct coder options for a float type', () => {
const result = getCoderFromTypeName('float');
expect(result.type.bits).toBe(32);
expect(result.type.fp).toBe(true);
});
it('should return correct coder options for a character type', () => {
const result = getCoderFromTypeName('char[4]');
expect(result.type.bits).toBe(8);
expect(result.size).toBe(4);
expect(result.formatter(65, 8)).toBe('A');
});
});
describe('decodeStruct', () => {
it('should decode a flat struct correctly from a hex array', () => {
const struct = {
field1: 'uint16',
field2: 'char[3]',
};
const hexArray = new Uint8Array([0x01, 0x0, 0x61, 0x62, 0x63]);
const result = decodeStruct({ struct, hexArray });
expect(result.field1).toBe(1);
expect(result.field2).toBe('abc');
});
it('should decode a nested struct correctly', () => {
const struct = {
type: 'int8',
header: {
version: 'uint8',
type: 'uint16be',
},
payload: {
value: 'floatbe',
},
};
const hexArray = new Uint8Array([0xFF, 0x01, 0x00, 0x02, 0x3F, 0x80, 0x0, 0x0]); // float 1.0
const result = decodeStruct({ struct, hexArray });
expect(result).toEqual({
type: -1,
header: { version: 1, type: 2 },
payload: { value: 1.0 },
});
});
it('should throw on bad buffer length', () => {
const struct = {
type: 'int8',
header: {
version: 'uint8',
type: 'uint16be',
},
payload: {
value: 'floatbe',
},
};
const hexArray = new Uint8Array([0xFF, 0x01, 0x00, 0x02, 0x3F, 0x80]); // missing last two bytes
expect(() => decodeStruct({ struct, hexArray })).toThrowError(
'Bad buffer length reading value(floatbe) at offset 4',
);
});
it('should throw an error when decoding an unsupported array type', () => {
const struct = {
list: ['uint8'],
};
const hexArray = new Uint8Array([0x01, 0x02, 0x03]);
expect(() => decodeStruct({ struct, hexArray })).toThrowError(
'Cannot decode a struct with array',
);
});
it('should throw an error when decoding an unsized array type', () => {
const struct = {
list: 'uint8[]',
};
const hexArray = new Uint8Array([0x01, 0x02, 0x03]);
expect(() => decodeStruct({ struct, hexArray })).toThrowError(
'Unsupported unsized array: uint8[]',
);
});
});
describe('encodeStruct', () => {
it('should encode a flat struct correctly', () => {
const struct = {
field1: 'uint8',
field2: 'char[3]',
};
const jsonObject = {
field1: 1,
field2: 'abc',
};
const result = encodeStruct({ struct, jsonObject });
expect(result).toEqual(new Uint8Array([0x01, 0x61, 0x62, 0x63]));
});
it('should encode a nested struct correctly', () => {
const struct = {
type: 'int8',
header: {
version: 'uint8',
type: 'uint16be',
},
payload: {
value: 'floatbe',
},
};
const jsonObject = {
type: -1,
header: { version: 1, type: 2 },
payload: { value: 1.0 },
};
const result = encodeStruct({ struct, jsonObject });
expect(result).toEqual(new Uint8Array([0xFF, 0x01, 0x00, 0x02, 0x3F, 0x80, 0x0, 0x0]));
});
it('should throw an error if array size is incorrect', () => {
const struct = {
field1: 'uint8',
field2: 'char[3]',
};
const jsonObject = {
field1: 1,
field2: 'abcd', // Too long
};
expect(() => encodeStruct({ struct, jsonObject })).toThrowError(
'Unexpected array size \'field2\'=\'97,98,99,100\' expected 3 elements',
);
});
it('should throw an error if value type is incorrect', () => {
const struct = {
field1: 'uint8',
field2: 'char[3]',
};
const jsonObject = {
field1: 1,
field2: 123, // Invalid type
};
expect(() => encodeStruct({ struct, jsonObject })).toThrowError(
'Unexpected non array \'field2\'=\'123\'',
);
});
it('should throw an error if value type is incorrect', () => {
const struct = {
field1: 'uint8',
field2: 'int16',
};
const jsonObject = {
field1: 1,
field2: '123', // Invalid type
};
expect(() => encodeStruct({ struct, jsonObject })).toThrowError(
'Unexpected array size \'field2\'=\'49,50,51\' expected 1 elements',
);
});
it('should encode multiple integer types correctly', () => {
const struct = {
a: 'uint8',
b: 'uint16',
c: 'uint32',
};
const jsonObject = {
a: 1,
b: 513,
c: 67305985,
};
const result = encodeStruct({ struct, jsonObject });
expect(result).toEqual(new Uint8Array([0x01, 0x01, 0x02, 0x01, 0x02, 0x03, 0x04]));
});
});
@@ -0,0 +1,193 @@
import { pack, unpack } from 'byte-data';
export type Conversion = 'dec' | 'bin' | 'hex' | 'char';
export function cleanHex(hex: string): string {
return hex.replace(/\\x|0x/g, '');
}
export function decodeNumber(n: number, bits: number, conv: Conversion) {
if (conv === 'bin') {
return n.toString(2).padStart(bits, '0');
}
if (conv === 'hex') {
return n.toString(16).padStart(bits / 4, '0');
}
if (conv === 'char') {
return String.fromCodePoint(n);
}
return n;
}
export function parseNumber(input: string | number): number | number[] {
if (!input) {
return 0;
}
if (typeof input === 'number') {
return input;
}
if (/^0x[0-9a-fA-F]+$/.test(input)) {
return Number.parseInt(input.substring(2), 16); // Parse as hexadecimal
}
else if (/^0b[01]+$/.test(input)) {
return Number.parseInt(input.substring(2), 2); // Parse as binary
}
return [...input].map(c => c.codePointAt(0) || 0);
}
function mergeModelAndObject(model: Record<string, any>, object: Record<string, any>): Record<string, any> {
const merged: Record<string, any> = {};
for (const key in model) {
if (Object.prototype.hasOwnProperty.call(model, key)) {
if (Array.isArray(object[key])) {
merged[key] = [model[key], object[key].map(parseNumber)];
}
else if (typeof object[key] === 'object' && !Array.isArray(object[key])) {
merged[key] = mergeModelAndObject(model[key], object[key]);
}
else {
merged[key] = [model[key], parseNumber(object[key])];
}
}
}
return merged;
}
interface CoderOption {
type: {
bits: number
fp?: boolean
be?: boolean
signed?: boolean
}
size: number
formatter: (n: number, bits: number) => string | number
join?: boolean
};
export function getCoderFromTypeName(typeName: string): CoderOption {
if (typeName.includes('[]')) {
throw new Error(`Unsupported unsized array: ${typeName}`);
}
const [, prefix, baseTypeName, bigEndian, arraySize] = /^((?:0x|0b)?)(u?int\d+|w?char|half|float|double)(be)?(?:\[(\d+)\])?$/.exec(typeName) || [];
let conv = 'dec';
if (prefix === '0x') {
conv = 'hex';
}
if (prefix === '0b') {
conv = 'bin';
}
const arraySizeNumber = Number.isNaN(Number(arraySize)) ? 1 : Number(arraySize);
if (baseTypeName === 'char' || baseTypeName === 'wchar') {
return {
type: {
bits: baseTypeName === 'char' ? 8 : 16,
be: !!bigEndian,
},
size: arraySizeNumber,
formatter: n => String.fromCodePoint(n),
join: true,
};
}
if (baseTypeName === 'float' || baseTypeName === 'double' || baseTypeName === 'half') {
return {
type: {
bits: baseTypeName === 'float' ? 32 : (baseTypeName === 'double' ? 64 : 16),
be: !!bigEndian,
fp: true,
},
size: arraySizeNumber,
formatter: n => n,
};
}
const [, unsigned, bits] = /^(u?)int(\d+)$/.exec(baseTypeName || '') || [];
return {
type: {
bits: Number(bits),
be: !!bigEndian,
signed: !unsigned,
},
size: arraySizeNumber,
formatter: (n, bits) => decodeNumber(n, bits, conv as Conversion),
};
}
export function decodeStruct({ struct, hexArray }: { struct: object; hexArray: Uint8Array }) {
let offset = 0;
const readMember = (obj: any) => {
const result: Record<string, any> = {};
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
if (Array.isArray(obj[key])) {
throw new TypeError(`Cannot decode a struct with array (key=${key}). Must be expressed as string with fixed length`);
}
else if (typeof obj[key] === 'object' && !Array.isArray(obj[key])) {
result[key] = readMember(obj[key]);
}
else {
const coderOption = getCoderFromTypeName(obj[key]);
const arr = [];
for (let i = 0; i < coderOption.size; i++) {
const dataSize = Math.ceil(coderOption.type.bits / 8);
if (offset + dataSize > hexArray.length) {
throw new Error(`Bad buffer length reading ${key}(${obj[key]}) at offset ${offset}`);
}
arr.push(coderOption.formatter(unpack(hexArray, coderOption.type, offset), coderOption.type.bits));
offset += dataSize;
}
if (coderOption.join) {
result[key] = arr.join('');
}
else {
result[key] = coderOption.size > 1 ? arr : arr[0];
}
}
}
}
return result;
};
return readMember(struct);
}
export function encodeStruct({ struct, jsonObject }: { struct: object; jsonObject: object }): Uint8Array {
const mergedObject = mergeModelAndObject(struct, jsonObject);
let buffer: Array<number> = [];
const writeMember = (obj: any) => {
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
if (typeof obj[key] === 'object' && !Array.isArray(obj[key])) {
writeMember(obj[key]);
}
else if (Array.isArray(obj[key])) {
const [typeName, value] = obj[key];
const coderOption = getCoderFromTypeName(typeName);
if (coderOption.size > 1 && !Array.isArray(value)) {
throw new TypeError(`Unexpected non array '${key}'='${value}'`);
}
if (Array.isArray(value) && value.length !== coderOption.size) {
throw new TypeError(`Unexpected array size '${key}'='${value}' expected ${coderOption.size} elements`);
}
const valueArr = !Array.isArray(value) ? [value] : value;
for (let i = 0; i < coderOption.size; i++) {
buffer = [...buffer, ...pack(valueArr[i], coderOption.type)];
}
}
else {
throw new TypeError(`Unexpected '${key}'='${obj[key]}'`);
}
}
}
};
writeMember(mergedObject);
return new Uint8Array(buffer);
}
+303
View File
@@ -0,0 +1,303 @@
<script setup lang="ts">
import hexArray from 'hex-array';
import { packArray, packString, unpackArray, unpackString } from 'byte-data';
import JSON5 from 'json5';
import { type Conversion, cleanHex, decodeNumber, decodeStruct, encodeStruct } from './hex-converter.service';
import { useValidation } from '@/composable/validation';
const mode = ref<'simple' | 'struct'>('simple');
const bits = ref(32);
const floatingPoint = ref(false);
const signed = ref(false);
const bigEndian = ref(false);
const decodeAs = ref<'dec' | 'bin' | 'hexa' | 'char' | 'utf8'>('dec');
const uppercase = ref(false);
const grouping = ref(1);
const rowlength = ref(0);
const hexInput = ref('');
const decodedOutput = computed(() => {
try {
const buffer = hexArray.fromString(cleanHex(hexInput.value));
if (decodeAs.value === 'utf8') {
return unpackString(buffer);
}
else {
return unpackArray(buffer, {
bits: bits.value,
fp: floatingPoint.value,
signed: signed.value,
be: bigEndian.value,
}, 0, buffer.length, true).map(n => decodeNumber(n, bits.value, decodeAs.value as Conversion)).join(' ');
}
}
catch (e: any) {
return e.toString();
}
});
const numberInput = ref('');
const encodedOutput = computed(() => {
try {
const values = numberInput.value.split(/\s+/).map(Number);
return hexArray.toString(
new Uint8Array(
packArray(values, {
bits: bits.value,
fp: floatingPoint.value,
signed: signed.value,
be: bigEndian.value,
})),
{
uppercase: uppercase.value,
grouping: grouping.value,
rowlength: rowlength.value,
},
);
}
catch (e: any) {
return e.toString();
}
});
const stringInput = ref('');
const utf8Output = computed(() => {
try {
return hexArray.toString(
new Uint8Array(packString(stringInput.value)),
{
uppercase: uppercase.value,
grouping: grouping.value,
rowlength: rowlength.value,
},
);
}
catch (e: any) {
return e.toString();
}
});
const structDefinition = ref(`{
x: "int32",
y: "int32",
}`);
const structDefinitionValidation = useValidation({
source: structDefinition,
rules: [
{
message: 'Struct definition is not a valid JSON',
validator: value => JSON5.parse(value.trim()),
},
],
});
const hexStructInput = ref('');
const decodedStructOutput = computed(() => {
try {
return JSON.stringify(
decodeStruct({
struct: JSON5.parse(structDefinition.value),
hexArray: hexArray.fromString(cleanHex(hexStructInput.value)),
}),
null, 2);
}
catch (e: any) {
return e.toString();
}
});
const jsonStructInput = ref('');
const encodedStructOutput = computed(() => {
try {
return hexArray.toString(
encodeStruct({
struct: JSON5.parse(structDefinition.value),
jsonObject: JSON5.parse(jsonStructInput.value),
}), {
uppercase: uppercase.value,
grouping: grouping.value,
rowlength: rowlength.value,
});
}
catch (e: any) {
return e.toString();
}
});
</script>
<template>
<div>
<n-radio-group v-model:value="mode" name="radiogroup" mb-2 flex justify-center>
<n-space>
<n-radio
value="simple"
label="Simple Encoder/Decoder"
/>
<n-radio
value="struct"
label="C/C++ typed struct"
/>
</n-space>
</n-radio-group>
<div v-if="mode === 'simple'">
<c-card title="Hex Options" mb-1>
<c-select
v-model:value="decodeAs"
label="Decode/Encode As:"
label-position="left" mb-1
:options="[{ value: 'dec', label: 'Decimal' }, { value: 'bin', label: 'Binary' }, { value: 'hex', label: 'Hexadecimal' }, { value: 'char', label: 'Char/ASCII' }, { value: 'utf8', label: 'UTF8 string' }]"
/>
<n-space v-if="decodeAs !== 'utf8'" align="baseline" justify="center">
<n-form-item label="Bits:" label-placement="left">
<n-input-number v-model:value="bits" :min="1" style="width: 6em" />
</n-form-item>
<n-form-item>
<n-checkbox v-model:checked="floatingPoint">
Floating Point
</n-checkbox>
</n-form-item>
<n-form-item>
<n-checkbox v-model:checked="signed">
Signed
</n-checkbox>
</n-form-item>
<n-form-item>
<n-checkbox v-model:checked="bigEndian">
Big Endian
</n-checkbox>
</n-form-item>
</n-space>
</c-card>
<c-card title="Hex Data Decoder" mb-3>
<c-input-text
v-model:value="hexInput"
multiline
placeholder="Put your Hex data here..."
rows="2"
label="Hex Data to decode"
raw-text
mb-5
/>
<n-form-item label="Your decoded values:">
<textarea-copyable :value="decodedOutput" />
</n-form-item>
</c-card>
<c-card v-if="decodeAs !== 'utf8'" title="Hex Data Encoder" mt-3>
<c-input-text
v-model:value="numberInput"
multiline
placeholder="Put your Numbers array here..."
rows="2"
label="Numbers array to encode"
raw-text
mb-5
/>
<n-form-item label="Your encoded numbers array as Hex:">
<textarea-copyable :value="encodedOutput" />
</n-form-item>
</c-card>
<c-card v-if="decodeAs === 'utf8'" title="Hex UTF8 String Encoder" mt-3>
<c-input-text
v-model:value="stringInput"
multiline
placeholder="Put your text here..."
rows="5"
label="String to encode"
raw-text
mb-5
/>
<n-form-item label="Your encoded string as UTF8 Hex:">
<textarea-copyable :value="utf8Output" />
</n-form-item>
</c-card>
<c-card title="Hex Encoding Output" mt-1>
<n-space align="baseline" justify="center">
<n-form-item label="Uppercase" label-placement="left">
<n-switch v-model:value="uppercase" />
</n-form-item>
<n-form-item label="Group by" label-placement="left">
<n-input-number v-model:value="grouping" :min="0" style="width: 6em" mr-1 /> digits (0 = no grouping)
</n-form-item>
<n-form-item label="Split as rows by" label-placement="left">
<n-input-number v-model:value="rowlength" :min="0" style="width: 6em" mr-1 /> group of digits (0 = no rows)
</n-form-item>
</n-space>
</c-card>
</div>
<div v-if="mode === 'struct'">
<c-card title="Struct Definition">
<c-input-text
v-model:value="structDefinition"
multiline
placeholder="Put your Struct defintion here..."
rows="5"
label="C/C+ like struct definition"
raw-text
mb-5
:validation="structDefinitionValidation"
/>
<details>
<summary>Instructions</summary>
<n-p>
Define you struct definition in JSON format: keys = struct member names ; value = type
<br>
Types syntax: u?int{size}(be)? | float(be)? | double(be)? | char | wchar(be)? | &lt;type&gt;[{array size}]
<br>
where "u" means "unsigned" ; "be" means "Big Endian" ; {size} is number of bits ; {array size} fixed size for arrays
<br>
can prefix integer with 0x or 0b to display as hex and binary
</n-p>
</details>
</c-card>
<c-card title="Hex Struct Decoder" m-t-1>
<c-input-text
v-model:value="hexStructInput"
multiline
placeholder="Put your Hex data here..."
rows="5"
label="Hex Data to decode"
raw-text
mb-5
/>
<n-form-item label="Your decoded values:">
<textarea-copyable :value="decodedStructOutput" />
</n-form-item>
</c-card>
<c-card title="Hex Struct Encoder" m-t-1>
<c-input-text
v-model:value="jsonStructInput"
multiline
placeholder="Put your Struct to encode here..."
rows="5"
label="Struct json to encode"
raw-text
mb-5
/>
<n-form-item label="Your encoded struct as Hex:">
<textarea-copyable :value="encodedStructOutput" />
</n-form-item>
</c-card>
<c-card title="Hex Encoding Output" mt-1>
<n-space align="baseline" justify="center">
<n-form-item label="Uppercase" label-placement="left">
<n-switch v-model:value="uppercase" />
</n-form-item>
<n-form-item label="Group by" label-placement="left">
<n-input-number v-model:value="grouping" :min="0" style="width: 6em" mr-1 /> digits (0 = no grouping)
</n-form-item>
<n-form-item label="Split as rows by" label-placement="left">
<n-input-number v-model:value="rowlength" :min="0" style="width: 6em" mr-1 /> group of digits (0 = no rows)
</n-form-item>
</n-space>
</c-card>
</div>
</div>
</template>
+12
View File
@@ -0,0 +1,12 @@
import { Binary } from '@vicons/tabler';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'Hex Encoder/Decoder',
path: '/hex-converter',
description: 'Encode and decode Hex buffers to number (bits, endianess, sign or floating point or chars) and structures',
keywords: ['hex', 'encode', 'decode', 'endianess', 'float', 'bits', 'hex', 'struct'],
component: () => import('./hex-converter.vue'),
icon: Binary,
createdAt: new Date('2025-02-09'),
});
+2 -2
View File
@@ -2,7 +2,7 @@ import { tool as base64FileConverter } from './base64-file-converter';
import { tool as base64StringConverter } from './base64-string-converter';
import { tool as basicAuthGenerator } from './basic-auth-generator';
import { tool as emailNormalizer } from './email-normalizer';
import { tool as currencyConverter } from './currency-converter';
import { tool as hexConverter } from './hex-converter';
import { tool as asciiTextDrawer } from './ascii-text-drawer';
@@ -99,6 +99,7 @@ export const toolsByCategory: ToolCategory[] = [
components: [
dateTimeConverter,
baseConverter,
hexConverter,
romanNumeralConverter,
base64StringConverter,
base64FileConverter,
@@ -116,7 +117,6 @@ export const toolsByCategory: ToolCategory[] = [
tomlToYaml,
xmlToJson,
jsonToXml,
currencyConverter,
markdownToHtml,
],
},