mirror of
https://git.um-react.app/um/um-react.git
synced 2025-11-28 03:23:02 +00:00
Co-authored-by: 鲁树人 <lu.shuren@um-react.app> Co-committed-by: 鲁树人 <lu.shuren@um-react.app>
16 lines
456 B
TypeScript
16 lines
456 B
TypeScript
export function formatHex(value: number, len = 8) {
|
|
return '0x' + (value | 0).toString(16).padStart(len, '0');
|
|
}
|
|
|
|
export function hex(value: Uint8Array): string {
|
|
return Array.from(value, (byte) => byte.toString(16).padStart(2, '0')).join('');
|
|
}
|
|
|
|
export function unhex(value: string): Uint8Array {
|
|
const bytes = [];
|
|
for (const [byte] of value.matchAll(/[0-9a-fA-F]{2}/g)) {
|
|
bytes.push(parseInt(byte, 16));
|
|
}
|
|
return new Uint8Array(bytes);
|
|
}
|