12 lines
413 B
TypeScript
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('')
|
|
}
|