Skip to content

Instantly share code, notes, and snippets.

@KDCinfo
Last active October 2, 2017 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KDCinfo/be7b9dbd42c6cc3c0e4ae79bfae9ffb9 to your computer and use it in GitHub Desktop.
Save KDCinfo/be7b9dbd42c6cc3c0e4ae79bfae9ffb9 to your computer and use it in GitHub Desktop.
Convert0101 - JavaScript: Convert between ASCII (char), ASCII (int), and 8-bit (Byte)

Convert0101 (JavaScript)

Convert between ASCII (char), ASCII (int), && 8-bit (Byte)

Added: Ability to update the default text being converted.

Code Crux

    /* Convert to ASCII (int) */
    word[idx] = '[' + ltr.charCodeAt().toString() + ']';

    /* Convert to 8-bit Bytes */
    word[idx] = '[' + ("000000000" + ltr.charCodeAt().toString(2)).substr(-8) + ']';

In the Wild

body {
color: #9a5b0b;
font-family: 'Sarpanch', sans-serif;
/*font-family: 'Baloo Thambi', cursive;*/
/*font-family: 'Baumans', cursive;*/
font-size: 12px;
}
#convert1111 {
/* Turn off 'hint' message initially */
display: none;
}
h1,
h2,
div {
margin: 0 0 0.5rem;
padding: 0rem;
}
<h1>Convert0101</h1>
<h2>Convert between ASCII (char), ASCII (int), &amp;&amp; 8-bit (Byte)</h2>
<div><a id="convert0101" href="#convert0101" onclick="convert0101(); return false;"></a> <small id="convert1111">(&larr; hint: click it...)</small></div>
<div><small id="convert0000">(plays twice - <a href="#convert0000" onclick="stopAutoConvert(); return false;">make it stop!)</a></small></div>
<div><input type="text" onkeyup="updateIt(this.value)" placeholder="Thanks!"></div>
let initText = "Thanks!";
let whichConvert = 0, // ['ASCII (char)', 'ASCII (int)', '8-bit (Byte)']
count = 0,
nIntervId;
window.onload = autoChange();
function autoChange() {
convert0101(); // Execute it initially, then `setTimer` will take over.
nIntervId = setInterval(convert0101, 3000);
}
function stopAutoConvert() {
// Clear the setInterval and show/hide the appropriate inline messages.
clearInterval(nIntervId);
document.getElementById('convert0000').style.display = 'none';
document.getElementById('convert1111').style.display = 'inline';
}
function convert0101() {
if (count == 5) { // Let it go 2 times around.
stopAutoConvert();
}
count++; // Count will simply forever increase (but never again hit 5).
if (whichConvert == 0) {
// If whichConvert is 0; Set text as initial text
// And move `whichConvert` to 1 (for ASCII (int))
document.getElementById("convert0101").innerText = initText;
whichConvert = 1;
} else {
// If whichConvert is 1 || 2 then
// use either ASCII (int) || 8-bit conversion methods
const newText = initText
.split("")
.reduce( (word, ltr, idx) => {
if (whichConvert == 1) {
/* Convert to ASCII */
word[idx] = '' + ltr.charCodeAt().toString() + '_';
} else {
/* Convert to 1-Byte bits */
word[idx] = '' + ("000000000" + ltr.charCodeAt().toString(2)).substr(-8) + '_';
}
return word;
}, []).join().replace(/,/g, '');
document.getElementById("convert0101").innerText = newText.substr(0, newText.length-1);
// If whichConvert is currently 1 (ASCII (int)),
// then set to 2; Else reset back to 0
whichConvert = whichConvert == 1 ? 2 : 0;
}
}
function updateIt(newVal) {
stopAutoConvert();
initText = newVal;
whichConvert = 0;
convert0101();
}
function byteString(dec) {
// `byteString()` attribution: [Ray Toal](https://stackoverflow.com/a/24337276/638153)
// Although now used inline (due to limited sample), this SO answer was quite helpful.
if (dec < 0 || dec > 255 || dec % 1 !== 0) {
throw new Error(dec + " does not fit in a byte");
}
return ("000000000" + dec.toString(2)).substr(-8)
}
/* Additional Inspiration:
https://www.google.com/search?q=javascript+mdn+int+char
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce?v=a
https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment