Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs convert string into UTF-8

Tags:

node.js

utf-8

From my DB im getting the following string:

Johan Öbert 

What it should say is:

Johan Öbert 

I've tried to convert it into utf-8 like so:

nameString.toString("utf8"); 

But still same problem.

Any ideas?

like image 649
Alosyius Avatar asked Nov 24 '13 12:11

Alosyius


People also ask

How do I encode a string to UTF-8 in Node?

nameString. toString("utf8");

How to convert text to UTF-8 in JavaScript?

The Best Answer isUse the utf8 module from npm to encode/decode the string. Encodes any given JavaScript string (string) as UTF-8, and returns the UTF-8-encoded version of the string. It throws an error if the input string contains a non-scalar value, i.e. a lone surrogate.

What is UTF-8 in Nodejs?

Overview. In this guide, you can learn how to enable or disable the Node. js driver's UTF-8 validation feature. UTF-8 is a character encoding specification that ensures compatibility and consistent presentation across most operating systems, applications, and language character sets.


2 Answers

Use the utf8 module from npm to encode/decode the string.

Installation:

npm install utf8 

In a browser:

<script src="utf8.js"></script> 

In Node.js:

const utf8 = require('utf8'); 

API:

Encode:

utf8.encode(string) 

Encodes any given JavaScript string (string) as UTF-8, and returns the UTF-8-encoded version of the string. It throws an error if the input string contains a non-scalar value, i.e. a lone surrogate. (If you need to be able to encode non-scalar values as well, use WTF-8 instead.)

// U+00A9 COPYRIGHT SIGN; see http://codepoints.net/U+00A9 utf8.encode('\xA9'); // → '\xC2\xA9' // U+10001 LINEAR B SYLLABLE B038 E; see http://codepoints.net/U+10001 utf8.encode('\uD800\uDC01'); // → '\xF0\x90\x80\x81' 

Decode:

utf8.decode(byteString) 

Decodes any given UTF-8-encoded string (byteString) as UTF-8, and returns the UTF-8-decoded version of the string. It throws an error when malformed UTF-8 is detected. (If you need to be able to decode encoded non-scalar values as well, use WTF-8 instead.)

utf8.decode('\xC2\xA9'); // → '\xA9'  utf8.decode('\xF0\x90\x80\x81'); // → '\uD800\uDC01' // → U+10001 LINEAR B SYLLABLE B038 E 

Resources

like image 36
Jayram Avatar answered Sep 19 '22 15:09

Jayram


I'd recommend using the Buffer object:

var someEncodedString = Buffer.from('someString', 'utf-8').toString(); 

This avoids any unnecessary dependencies that other answers require, since Buffer is included with node.js, and is already defined in the global scope.

like image 130
Lord Elrond Avatar answered Sep 19 '22 15:09

Lord Elrond