Can You please help mу how to encode non-latin (russian) letters, that are mixed with special symbols, for example: Abc Ø абв
(here is english text, special symbol ‘latin o’ and russian text).
I have existing RTF template with ‘placeholder’ text inside, and what I need is to replace this ‘placeholder’ with ‘Abc Ø абв’:
I use function from here, at the bottom of the page to decode UTF-8 to Win-1251 – it successfully writes russian letters but finally I get “Ш” Instead of ‘Ø‘:
Here is my example code and input and output files:
input rtf: https://mega.nz/file/CtNB2CiY#yid1nLq9P6Jo8zSRAsXeGai-mZLV6xP1OvN1jDpFyG4
output rtf generated by the code below: https://mega.nz/file/asMExKJI#q8oRn1J9oWMlUck6tJ6MdpVGiIjt81kNFRo7T3eSBTU
const http = require('http');
const port = 3100;
function utf8_decode_to_win1251(srcStr) {
var tgtStr = "",
c = 0;
for (var i = 0; i < srcStr.length; i++) {
c = srcStr.charCodeAt(i);
if (c > 127) {
if (c > 1024) {
if (c === 1025) {
c = 1016;
} else if (c === 1105) {
c = 1032;
}
c -= 848;
}
// c = c % 256; // ???
}
tgtStr += String.fromCharCode(c);
}
return tgtStr;
}
const server = http.createServer(function (req, res) {
const fs = require('fs');
// read existing file
fs.readFile("C:input.rtf", "utf8", (err, inputText) => {
if (err) {
console.error(err);
return;
}
// I want to replace 'placeholder' text in file with this test text:
let text = `Abc Ø абв`; // 'Abc Ø абв'
text = utf8_decode_to_win1251(text); // text with encoded russian letters 'Abc Ø àáâ'
// replace placeholder from input RTF with text with non-latin characters 'Abc Ø àáâ':
inputText = inputText.replace("placeholder", text);
// RTF uses 8-bit so need to convert from unicode
let buf = Buffer.from(inputText, "ascii"); // "binary" also gives wrong output text https://stackoverflow.com/a/34476862/348736
// write output file to disk
fs.writeFile("C:output.rtf", buf, function (error, resultFile) { // result file contains 'Abc Ш абв', which is wrong..
if (!error) {
console.info('Created file', resultFile);
}
else {
console.error(error);
}
});
});
});
server.listen(port, function (error) {
if (error) {
console.log(`${error}`);
} else {
console.log(`listening on port ${port}`);
}
})