Last active
October 6, 2018 16:24
-
-
Save OmarShehata/a8b4edf25af99f855fc2e36550e2976d to your computer and use it in GitHub Desktop.
Sample test of Javascript Typed Arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Javascript Typed Arrays</title> | |
<meta charset="UTF-8"> | |
<script type="text/javascript" src="https://fastcdn.org/FileSaver.js/1.1.20151003/FileSaver.min.js"></script> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
// Here's a sample string | |
let sample_string = "Hello World!" | |
// Get the raw numbers to save | |
let bytes = [] | |
for(let i = 0;i < sample_string.length; i++){ | |
bytes.push(sample_string[i].charCodeAt(0)) | |
} | |
// Put the numbers in 2 bytes each | |
let u16 = new Uint16Array(bytes) | |
// Save the string directly | |
// try putting u16 instead of sample_string below, and compare the outputs. | |
saveAs(new Blob([sample_string]),"output.txt") | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment