Beispiel:JS-Window-atob-Unicode.html

Aus SELFHTML-Wiki
Wechseln zu: Navigation, Suche
<!doctype html>
<html lang="de">
<head>
<title>atob</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./Beispiel:SELFHTML-Beispiel-Grundlayout.css" />
<script>
document.addEventListener('DOMContentLoaded', function(evt) {
	document.getElementById('encu').addEventListener('click', decode);
});

function decode() {
	let s = document.getElementById('einu').value;
	let uc = new Uint16Array(s.length);

	for (let i = 0; i < uc.length; ++i) {
		uc[i] = s.charCodeAt(i);
	}

	let b64 = btoa(String.fromCharCode(...new Uint8Array(uc.buffer)));
	document.getElementById('ausb64').textContent = b64;

	let a64 = atob(b64);
	document.getElementById('ausb').value = a64;
	let b = new Uint8Array(a64.length);

	for (let i = 0; i < b.length; ++i) {
		b[i] = a64.charCodeAt(i);
	}

	document.getElementById('ausu').value =
		String.fromCharCode(...new Uint16Array(b.buffer));
}
</script>
</head>
<body>
<h1>atob</h1>
<form>
<p><label>Klartext: <input type="text" id="einu"/></label><p>
<button id="encu" type="button">Encodieren</button>
<p><label>Base64: <output id="ausb64"></output></p>
<p><label>Binär: <output id="ausb"></output></p>
<p><label>Unicode: <output id="ausu"></output></p>
</form>
</body>
</html>