Skip to content

Instantly share code, notes, and snippets.

@OmarShehata
Last active October 6, 2018 16:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OmarShehata/a8b4edf25af99f855fc2e36550e2976d to your computer and use it in GitHub Desktop.
Save OmarShehata/a8b4edf25af99f855fc2e36550e2976d to your computer and use it in GitHub Desktop.
Sample test of Javascript Typed Arrays
<!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