Ok, after a couple of turns it seems like the support for binary string
encoding in Buffer's will go away, still I believe that it can be a better
way to represent binary data in JSON instead of base64 in some
circumstances, for example when transparently shuffling HTTP entityBodies
forward and back using various transports, e.g JSON (in this case using the
faye bayeux implementation).
So I wonder, how can this be done better in absence of 'binary':
function hexdump(data) {
var i, line = "hexdump, length: " + data.length, len, m, str = false;
str = typeof data === "string" ? true : false;
len = data.length;
for(i = 0; i < len; i++) {
m = i & 0xf;
if(m === 0) {
console.log(line);
line = ("0000"+Number(i).toString(16) ).slice(-4) + " ";
} else if(m === 8) {
line += "-";
} else {
line += " ";
}
line += ("00"+Number(str ? data.charCodeAt(i) : data[i]).toString(16)
).slice(-2);
}
console.log(line);
}
var i, b = new Buffer(256);
for(i = 0; i < 256; i++) {
b[i] = i;
}
hexdump(b);
var hex = b.toString('hex');
function stat(str) {
return "len: " + str.length + ", str: " + str;
}
var bin = "";
// in absence of str = b.toString('binary');
for(i = 0; i < b.length; i++) {
bin += String.fromCharCode(b[i]);
}
console.log(stat(JSON.stringify( { bin: bin } )));
var b64 = b.toString('base64');
console.log(stat(JSON.stringify( { b64: b64 } )));
var b2 = new Buffer(bin.length);
// in absence of b = new Buffer(str, 'binary');
for(i = 0; i < 256; i++) {
b2[i] = bin.charCodeAt(i);
}
hexdump(b2);
What this example tries to achieve is a conversion back and forth of a
chunk of binary data using strings, which is then serialized using JSON.
Note that the inline hexdump tool uses charAt as a workaround when dumping
binary strings instead of array indexing.
Anyway, the example shows that base64 is the most efficient encoding for
random binary data, but the benefit of using binary strings is that string
processing is a cake when dealing with text content, and just a bit more
inefficient than base64 for transparent binary data.
So how can this be done more efficiently in node in the absence of 'binary'
? E.g without looping through bytewise and concatenating single char
strings.
//Mattias
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en