Skip to content

Instantly share code, notes, and snippets.

@BasixKOR
Last active February 15, 2020 07:55
Show Gist options
  • Save BasixKOR/34910e47e86b96cb1df37e244e3ba850 to your computer and use it in GitHub Desktop.
Save BasixKOR/34910e47e86b96cb1df37e244e3ba850 to your computer and use it in GitHub Desktop.
Handling codepage

Normalizing Codepage in Node.js

Only tested in CP-949.

Note

  • This will only work on the codepages below.
    • Windows codepages: 874, 1250-1258
    • IBM codepages: 437, 737, 775, 808, 850, 852, 855-858, 860-866, 869, 922, 1046, 1124, 1125, 1129, 1133, 1161-116
    • Multi-byte: 932, 936, 949, 950
import Iconv from 'iconv-lite'
import child_process from 'child_process'
child_process.exec('chcp.com', { encoding: 'buffer' }, (err, stdout, stderr) => {
if(err) throw err;
const [ codepage ] = stdout.toString().match(/\d+/)
const buf = Iconv.decode(Buffer.from(stdout), 'cp'+codepage)
console.log(buf)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment