String-ArrayBuffer conversion
This commit is contained in:
parent
daef7ca9bd
commit
976d7a28c9
3 changed files with 24 additions and 0 deletions
|
@ -17,3 +17,8 @@
|
||||||
* Andrew Kepert
|
* Andrew Kepert
|
||||||
* Ydnar
|
* Ydnar
|
||||||
* Lostinet
|
* Lostinet
|
||||||
|
|
||||||
|
|
||||||
|
## [String/ArrayBuffer conversion](http://updates.html5rocks.com/2012/06/How-to-convert-ArrayBuffer-to-and-from-String)
|
||||||
|
|
||||||
|
* Renato Mangini
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
"app": {
|
"app": {
|
||||||
"background": {
|
"background": {
|
||||||
"scripts": ["third-party/md5-min.js",
|
"scripts": ["third-party/md5-min.js",
|
||||||
|
"third-party/ab-str.js",
|
||||||
"lib/ns_client.js",
|
"lib/ns_client.js",
|
||||||
"background.js"
|
"background.js"
|
||||||
]
|
]
|
||||||
|
|
18
third-party/ab-str.js
vendored
Normal file
18
third-party/ab-str.js
vendored
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
//
|
||||||
|
// Renato Mangini
|
||||||
|
// http://updates.html5rocks.com/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
|
||||||
|
// http://stackoverflow.com/questions/6965107/converting-between-strings-and-arraybuffers
|
||||||
|
//
|
||||||
|
|
||||||
|
function ab2str(buf) {
|
||||||
|
return String.fromCharCode.apply(null, new Uint16Array(buf));
|
||||||
|
}
|
||||||
|
|
||||||
|
function str2ab(str) {
|
||||||
|
var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
|
||||||
|
var bufView = new Uint16Array(buf);
|
||||||
|
for (var i=0, strLen=str.length; i<strLen; i++) {
|
||||||
|
bufView[i] = str.charCodeAt(i);
|
||||||
|
}
|
||||||
|
return buf;
|
||||||
|
}
|
Reference in a new issue