Converting a string from utf8 to latin1 in NodeJS

antjanus picture antjanus · Feb 18, 2015 · Viewed 8.5k times · Source

I'm using a Latin1 encoded DB and can't change it to UTF-8 meaning that I run into issues with certain application data. I'm using Tesseract to OCR a document (tesseract encodes in UTF-8) and tried to use iconv-lite; however, it creates a buffer and to convert that buffer into a string. But again, buffer to string conversion does not allow "latin1" encoding.

I've read a bunch of questions/answers; however, all I get is setting client encoding and stuff like that.

Any ideas?

Answer

adeneo picture adeneo · Feb 18, 2015

You can create a buffer from the UFT8 string you have, and then decode that buffer to Latin 1 using iconv-lite, like this

var buff   = new Buffer(tesseract_string, 'utf8');
var DB_str = iconv.decode(buff, 'ISO-8859-1');