바이너리 데이터를 64진법 표현으로 변환하여 정해진 아스키 문자 집합으로 표현하는 바이너리-텍스트 인코딩 체계입니다.
바이너리 데이터를 전송할 수 없는 미디어에서 잔송 작업을 할 수 있도록 바이너리 데이터를 문자열로 인코딩하는데 사용됩니다.
const str = "JavaScript is fun!!";
const encodedStr = btoa(str);
// output: SmF2YVNjcmlwdCBpcyBmdW4hIQ==
const str = "JavaScript is fun 🎉";
const encodedStr = btoa(str);
// output: ?
function encodeUnicode(str) {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
function toSolidBytes(match, p1) {
return String.fromCharCode('0x' + p1);
}));
}
const encodedStr = "SmF2YVNjcmlwdCBpcyBmdW4hIQ==";
const str = atob(encodedStr);
// output: JavaScript is fun!!
const encodedStr = "SmF2YVNjcmlwdCBpcyBmdW4g8J+OiQ==";
const str = atob(encodedStr);
// output: JavaScript is fun 🎉
function decodeUnicode(str) {
// Going backwards: from bytestream, to percent-encoding, to original string.
return decodeURIComponent(atob(str).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}