From 976d7a28c9893b327959041b1aa25430b71237b3 Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Sun, 21 Oct 2012 20:26:41 +0200 Subject: [PATCH] String-ArrayBuffer conversion --- CREDITS.md | 5 +++++ manifest.json | 1 + third-party/ab-str.js | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 third-party/ab-str.js diff --git a/CREDITS.md b/CREDITS.md index 4bcb181..3d7bf16 100644 --- a/CREDITS.md +++ b/CREDITS.md @@ -17,3 +17,8 @@ * Andrew Kepert * Ydnar * Lostinet + + +## [String/ArrayBuffer conversion](http://updates.html5rocks.com/2012/06/How-to-convert-ArrayBuffer-to-and-from-String) + +* Renato Mangini diff --git a/manifest.json b/manifest.json index 50b32f1..a853aaf 100644 --- a/manifest.json +++ b/manifest.json @@ -7,6 +7,7 @@ "app": { "background": { "scripts": ["third-party/md5-min.js", + "third-party/ab-str.js", "lib/ns_client.js", "background.js" ] diff --git a/third-party/ab-str.js b/third-party/ab-str.js new file mode 100644 index 0000000..2bba23b --- /dev/null +++ b/third-party/ab-str.js @@ -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