wi2ard
wi2ard

Reputation: 1545

Unescape Unicode char in javascript

I am expecting to get a string containing non ascii chars, for eg. the string Øvo, but the string I get instead contains a different representation of that char: Ã\u0098vo. How can I convert this to the string I want?

I know how this string represention is obtained but I can't undo that encoding:

let modelString = JSON.stringify(model);
return window.btoa(unescape(encodeURIComponent(modelString))); 

I have been trying a lot of functions (from Firefox console) but I'm not getting the expected result:

Upvotes: 0

Views: 469

Answers (1)

Reflective
Reflective

Reputation: 3917

Avoid using unescape as it is "removed from the Web standards"!

var model = {"key": "Øvo"};
var modelString = JSON.stringify(model);
var uriEncoded = encodeURIComponent(modelString);

console.log(uriEncoded);
// wrong
console.log(unescape(uriEncoded));
// right
console.log(decodeURIComponent(uriEncoded));

// btoa convertion
var b2a = btoa(decodeURIComponent(uriEncoded));
console.log(b2a);
// the reverse conversion
var a2b = atob(b2a);
console.log(a2b);

Upvotes: 1

Related Questions