NodeJS compatible Buffer implementation. See original Buffer doumentation
Two additional methods are added for converting between any codepage:
-
Buffer.prototype.cpSlice(start, end, sourceEncodingNum): string
: convert a part of buffer, encoded assourceEncoding
into JavaScript stringconst xmlIn1251EncodingBin = fs.readFileSync(path.join(__dirname, 'win1251Encoded.XML'), {encoding: 'binary'}) const buffer = Buffer.from(xmlIn1251EncodingBin) const strData = buffer.cpSlice(0, buffer.length, 1251) // convert buffer, encoded as Windows1251 into string
-
Buffer.cpFrom(string, targetEncodingNum): Buffer
: convert a JavaScript string into buffer with specified encodingconst buffer = Buffer.cpFrom('here text in cyrilic', 1251) // result is Win-1251 encoded string in binary format