-
Notifications
You must be signed in to change notification settings - Fork 130
Converting results from arraybuffers to string values
Depre edited this page Apr 2, 2017
·
4 revisions
test6 = function(){
database.query("select id, name from users order by name",
function (err, rs) {
if (err) {logError(err); return}
// get results using array loop
for (i=0;i<rs.length;i++) {
console.log( rs[i].id, ab2str(rs[i].name));
}
console.log('------------------------------');
// alternatively, get results using foreach
rs.forEach(function(row){
console.log( row.id, ab2str(row.name));
});
}
);
};
[converting-between-strings-and-arraybuffers] (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;
}