mirror of
https://github.com/ershisan99/DevToysWeb.git
synced 2025-12-16 20:49:23 +00:00
15 lines
375 B
TypeScript
15 lines
375 B
TypeScript
export const encode = (s: string) => {
|
|
const bytes = new TextEncoder().encode(s);
|
|
return btoa(String.fromCharCode(...bytes));
|
|
};
|
|
|
|
export const decode = (base64: string) => {
|
|
try {
|
|
const binString = atob(base64);
|
|
const bytes = Uint8Array.from(binString, c => c.charCodeAt(0));
|
|
return new TextDecoder().decode(bytes);
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
};
|