Issue in decoding in node js

93 Views Asked by At

I have a requirement where I need to encode data in "iso-8859-1" and then convert back it to readable string in node js.

In .Net env:

string encodedData = "VABpAG0AZQAgAHMAZQByAGUAaQBzAA==";
Encoding encoding = Encoding.GetEncoding("iso-8859-1"); // encoding in "iso-8859-1"
byte[] = decodedbuff = convert.FromBase64String(encodedData);   // getting buffer
result = encoding.GetString(decodedbuff);    //decoding

result = timesereis

In a similar way, I need to encode and decode in node js

In Node js(using iconvlite)

const data = "VABpAG0AZQAgAHMAZQByAGUAaQBzAA=="
const buffer = iconvlite.encode(data,'iso-8859-1');
const result = buffer.toString('utf8');

Here in result, I am getting "VABpAG0AZQAgAHMAZQByAGUAaQBzAA==" instead of decoded result

1

There are 1 best solutions below

0
TArvela On

By using the following code you get your desired result

let buffer = new Buffer(data, 'base64');
let result = buff.toString('utf-8');

console.log("result: "+text)