Reputation: 11
i'm using node.js, to read data from a Barcode Scanner. So that is my code:
var HID = require('node-hid');
var usb = require('usb');
// Honeywell Scanner
var vid = 0xc2e;
var pid = 0xbe1;
var d = new HID.HID(vid, pid);
d.on("data", function (data) {
console.log(data);
});
d.on("error", function (error) {
console.log(error);
d.close();
});
My Problem is, that i get a Buffer that looks like < Buffer 00 00 00 00 00 00 00 00 >. After scanning a barcode (for example a barcode with the id 12) the console returns something like that
<Buffer 00 00 53 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 53 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 1e 00 00 00 00 00>
<Buffer 00 00 1f 00 00 00 00 00>
How can i convert this Buffer output into a readable string? In that case it would be a 12.
Thanks for your help!
Upvotes: 1
Views: 5001
Reputation: 982
I think what you want to do is to decode your data
buffer.
To decode a buffer, you simply use the built-in .toString() method, passing in the character encoding to decode to:
data.toString('hex'); //<-- Decodes to hexadecimal
data.toString('base64'); //<-- Decodes to base64
If you don't pass anything to toString
, utf8 will be the default.
EDIT
If you'd like to know which character encodings are currently supported by Node (other than hex, base64 and utf8), visit the official docs.
Upvotes: 6