Skip to content

Instantly share code, notes, and snippets.

@rebrec
Created November 24, 2016 17:25
Show Gist options
  • Save rebrec/21e0540ca0a121126db1aecafc426bad to your computer and use it in GitHub Desktop.
Save rebrec/21e0540ca0a121126db1aecafc426bad to your computer and use it in GitHub Desktop.
// TLDR: I am trying to convert (new Buffer([86,97,108,130,114,105,101])).toString('utf8') to "Valérie" (and not "Val�rie""
/*
I am using a node module which permit me to interact with a powershell console.
During the execution of a command, i encounter encoding problems
Here is an example of what i receive from the module
*/
var iconv = require('iconv-lite');
// The below variable is the kind of data that the module process. This data is received from the child process's stdout
var rawData = new Buffer([86,97,108,130,114,105,101]); // Supposed to be "Valérie"
// Inside this module, during the data processing of this module, the rawData is converted to UTF-8 using the above function
var dataStr = rawData.toString('utf8');
// This dataStr is then returned to my user code with the wrong encoding...
// If i manually launch a powershell console and execute the necessary command the retrieved string is "Valérie"
// But when i receive it from the node module, it is "Val�rie" and not "Valérie" ...
// I try to find a way to convert the data back to the correct encoding from what i have read, it should be cp437.
// I already tried to change inside powershell process thoses values :
// 'chcp 65001',
// '$OutputEncoding = [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding',
// '$OutputEncoding = [System.Text.Encoding]::GetEncoding(65001)',
// But this doesn't work at all...
// After looking at my array, it seems that the received char 'é' is coded as int 130.
// After some researches it seems to be cp437.
// So what i want to do is to convert the string received from my module's function (wrongly encoded to utf8)
// to cp437 (i guess...)
// I have found that the iconv-lite module could help me, but i can't find my way to handle it
// I must admin that all this encoding stuff is not perfectly clear in my mind, it has been like that for years and it seems
// that i am not able to solve this by myself...
// Hope some of you will
// Useful funcs
// var newConvertedBuffer = Buffer.from(dataStr,'utf8');
// I am trying to figure out how to convert the resulting value to what i expect ("Valérie")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment