seckit/src/utils/convert/index.ts
2026-04-15 16:10:29 +08:00

12 lines
413 B
TypeScript

// Hex 转 UTF8
export const hexToUtf8 = (hex: string): string => {
const bytes = new Uint8Array(hex.match(/.{1,2}/g)?.map(byte => parseInt(byte, 16)) || [])
return new TextDecoder('utf-8').decode(bytes)
}
// UTF8 转 Hex
export const utf8ToHex = (utf8: string): string => {
const bytes = new TextEncoder().encode(utf8)
return Array.from(bytes).map(byte => byte.toString(16).padStart(2, '0')).join('')
}