Search code examples
file-conversioniconvgnuwin32

Converting file using `iconv` from UTF-16LE to UTF-8 yields UTF-16LE file


Title.

The command I run is:

iconv -f UTF-16LE -t UTF-8 .\data\schema-utf16le.graphql > .\data\schema-utf8.graphql;

However, the generated file schema-utf8.graphql is still UTF-16LE encoded.

What am I doing wrong?

I am on windows and installed this version of iconv.


Solution

  • This question has been online for a long time and received literally no views nor an answer. Here's how I finally solved the problem.

    I made a script for nodejs which performs the conversion:

    const fs = require('fs');
    
    const schemaFileName = 'data/schema.graphql';
    
    const readContent = fs.readFileSync(schemaFileName, {
      encoding: 'utf16le',
    });
    
    const writeContent = (readContent.charAt(0) === '\ufeff')
      ? readContent.substring(1)
      : readContent;
    
    fs.writeFileSync(schemaFileName, writeContent, 'utf8');