Files
QZMusic_PC/amll-local/packages/lyric/src/formats/eqrc/utils.ts

40 lines
873 B
TypeScript
Raw Normal View History

/// <reference types="node" />
/**
* @module utils
* @description
*
*
*/
/**
* Uint8Array
*/
export function hexToUint8Array(hex: string): Uint8Array {
if (typeof Buffer !== "undefined") {
return Buffer.from(hex, "hex");
}
if (hex.length % 2 !== 0) {
throw new Error("无效的十六进制字符串: 长度必须是偶数");
}
const bytes = new Uint8Array(hex.length / 2);
for (let i = 0; i < hex.length; i += 2) {
bytes[i / 2] = parseInt(hex.substring(i, i + 2), 16);
}
return bytes;
}
/**
* Uint8Array
*/
export function uint8ArrayToHex(bytes: Uint8Array): string {
if (typeof Buffer !== "undefined") {
return Buffer.from(bytes).toString("hex");
}
return Array.from(bytes)
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
}