1

I created in my Linux PC a simple app that generates TXT files using fs NodeJS command. But I need read those files with an external Window app. So I need those files in ANSI encode. I installed Wine and for now, I open the file with notepad and save it again to convert the codification. There is any way to generate the TXT files with ANSI encoding from NodeJS directly?

Marcelo J Forclaz
  • 718
  • 1
  • 8
  • 27
  • 2
    If all of your text is using characters from 0 to 127 then UTF8 is ANSI. The special encoding only takes place for characters 128 and above. Otherwise you need to convert each character into the encoding you want and put it into a buffer, then write the buffer out. – Intervalia Dec 19 '18 at 15:54
  • No, Windows doesn't need ANSI encoding, it uses Unicode (UTF16) natively. So does .NET. UTF8 is identical to ANSI as well which means you don't need to convert anything. If you have a non-Unicode application that requires a *specific* codepage, you should find what that is and save your text with that codepage. – Panagiotis Kanavos Dec 19 '18 at 16:30

1 Answers1

2

I solved my question like this:

let txtContent = '' // string with content to save as file

const buffer = Buffer.from(txtContent, 'latin1')
fs.writeFile('files/file_name.txt')
Marcelo J Forclaz
  • 718
  • 1
  • 8
  • 27
  • You most definitedly *don't* need to use ANSI with Windows. Windows uses *Unicode* natively. So does .NET, which means any .NET applications understand Unicode. The default file encoding for text files is UTF8. If you have an application that needs a single-byte encoding you should **NOT** use ASCII otherwise you'll lose characters abote 127 that can't be represented in that codepage – Panagiotis Kanavos Dec 19 '18 at 16:28
  • BTW Intervalia said to *not* use `ascii` but find the correct codepage and use that. Otherwise you may lose non-English characters, like accented Latin1 characters – Panagiotis Kanavos Dec 19 '18 at 16:33
  • Thanks @PanagiotisKanavos for your comment. The TXT files contains only numbers, I don't care by losing Latin1 characters. However I replaced ```ascii``` by ```latin1``` in my code and works fine. The App for reading the TXT files is not writen in .NET It's very much older than it (VB5 I suspect) and it have a kind of data verification that refuse incorrect encoding. – Marcelo J Forclaz Dec 19 '18 at 19:29
  • 1
    VB5-6 also uses Unicode natively "but" - some programmers still used GWBasic-style file IO even in the 2000s. Even so, Intervalia's original advice is the best option. Save the numbers as UTF8. The file will be identical to ASCII or Latin1 precisely because the character range 0-127 is identical in all single-byte codepages *and* UTF8. Without UTF8, iIf you had non-English text you'd have to find what codepage the program wants, and what the system's locale is. *Non*-Unicode applications use the system's locale to determine the default codepage. – Panagiotis Kanavos Dec 20 '18 at 09:02