Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.