UTF-8 to readable characters [duplicate] - javascript

This question already has answers here:
Using Javascript's atob to decode base64 doesn't properly decode utf-8 strings
(12 answers)
Closed 4 years ago.
Encoding and decoding a string is not as easy as I thought.
The original string is as follows:
at the end of → al término de • después de
After PHP base64 encoding (used three times) it looks different:
VUVkSksxbFlVV2RrUjJoc1NVZFdkVnBEUW5aYWFuZDJXV28wWnpSdllWTkpSMFp6U1VoVVJIRllTblJoVnpWMlNVZFNiRWxQUzBGdmFVSnJXbGhPZDJSalQzQmplVUpyV2xSNGQxQm5QVDA9
When trying JS window.atob() to decode the string, the result is this:
at the end of â al término de ⢠después de
UTF-8 characters are not displayed properly. What function should I use to fix this?

Try
let decodedString = decodeURIComponent(escape(window.atob(yourString)))

What happens with this is that JavaScript does not allow characters that are outside the range of Latin1, to be able to execute it you could do the following:
let str = 'at the end of → at the end of • after';
btoa(unescape(encodeURIComponent(str)))
// output: YXQgdGhlIGVuZCBvZiDihpIgYXQgdGhlIGVuZCBvZiDigKIgYWZ0ZXI=
This code will convert (from javascript) the string correctly to base64 and from PHP you can decode it well, now the problem is that JavaScript will not be able to decode it since it interprets it as the syntax is wrong (although it has been encoded) and so It will not work for you either.

Related

printing unchanged string in JS [duplicate]

This question already has answers here:
Does JavaScript have literal strings?
(6 answers)
Javascript - How to show escape characters in a string? [duplicate]
(3 answers)
Closed 3 years ago.
In C# I can do the following:
String1 = "Test \r\n test!";
String2 = #"Test \r\n test!";
output String1:
Test
test!
output String2:
Test \r\n test!
In JavaScript I only found unescape(). But that is completly outdated and is not really what I was searching for, because that translated special characters to other things. I want that 'nothing' is translated, but everything is given out as it was in the string. Has someone an idea how I can do in JS what I can achieve in C# with the '#'?
JavaScript has no equivalent to C# verbatim string literals.
You need to escape special characters when creating the string.
const string1 = "Test \\r\\n test!";
Some people have suggested using JSON.stringify, but the initial parsing of the string will normalise it, so you can't reliably recover the original input.
For example, an escaped space means the same as a space on its own.
const input = "A string containing a \ character";
const output = JSON.stringify(input);
document.write(output);

JSON.parse without escaping [duplicate]

This question already has answers here:
How can I use backslashes (\) in a string?
(4 answers)
Closed 6 years ago.
Is there anyway to do this in JavaScript:
$ cat test.json
{"body":"\u0000"}
$ python3 -c 'import json; print(json.load(open("test.json", "r")))'
{'body': '\x00'}
Notice, the data above only one \ (does not need to be escaped). So you have the following situation in JavaScript:
JSON.parse('{"body":"\\u0000"}') // works
JSON.parse('{"body":"\u0000"}') // does not work
With potentially any UTF-8 data comming from a binary source (websocket), can this data be processed directly like in the first python example above?
String characters from \u0000 through \u001F are considered as control characters, and according to RFC-7159 are not allowed characters to use in JSON and must be escaped, as stated in section 7.
What you are trying to do is to put unescaped control characters into a JSON, which clearly not acceptable, you have to escape it first, non of the languages accept it, even Python.
The correct answer would be place a UTF-8 encoded value into a string containing a JSON format.
This is a correct JSON, and will be parsed by any JSON parser in any language, even in JavaScript:
{"body":"\u0000"}
This is incorrect JSON (consider the [NUL] as a NUL control character, as it cannot be represented in text):
{"body":"[NUL]"}
That's why JSON.parse('{"body":"\\u0000"}') works and JSON.parse('{"body":"\u0000"}') doesn't.
Hope, it clarifies what's wrong with your test.

How to escape double and single quote from JS string [duplicate]

This question already has answers here:
Javascript, Razor and Escape characters. Like apostrophe
(5 answers)
Closed 7 years ago.
I'm assigning the value of JS string variable from server side code.
Like this
var fbShareTitle = "#(ViewBag.LeadTitle as string)";
ViewBag return string value is
A "Fantastic" Lead With 'Qoute'
Now It is giving error in console
SyntaxError: missing ; before statement
I have tried this
var fbShareTitle = ("#(ViewBag.LeadTitle)").replace(/"/g, '\\"');
But now I'm getting this error.
SyntaxError: missing ; before statement
As This string will be shared on fb, So i can't modify string, like replace all " with ' e.t.c.
The reason why your code doesn't work is that Razor will generate the following:
var fbShareTitle = "A "Fantastic" Lead With 'Qoute'";
which is invalid JavaScript. You can't simply fix it by replace, since it's not the problem that your string is bad, it's that your code can't parse - replace never gets to execute. You need to fix it on serverside, where you generate the JavaScript in question, by modifying your Razor code:
var fbShareTitle = #Html.Raw(Json.Encode(ViewBag.LeadTitle as string));
Json will take care of quotes and proper escaping; Raw will make sure you don't get your < and > replaced. Extra benefit from #Html.Raw(Json.Encode(...)) mantra: you can use it to inject any kind of data that can be encoded in JSON, not only strings.

Javascript / jQuery: How to convert string to HTML character code [duplicate]

This question already has answers here:
How to convert characters to HTML entities using plain JavaScript
(12 answers)
Closed 8 years ago.
I have a string that contains text including special characters like apostrophes, quotes, ampersand etc. that I would like to convert to HTML character codes, e.g. to show &#39 instead of an apostrophe etc. (as per the following list: http://www.w3.org/MarkUp/html-spec/html-spec_13.html.
I do not necessarily need to convert letters and spaces as long as the above and some other basic characters are encoded correctly.
For a temporary workaround I use simple replaces like str = str.replace("'", "&#39") but would like to avoid this as it only covers certain characters.
I was thinking of encodeURI or encodeURIcomponent but would like to know what is the best and easiest way to achieve this without encoding more than necessary.
Can someone tell what is the best way here if the idea is to avoid any issues with other JS where this is inserted dynamically, esp. thinking of single quotes, double quotes and any other character that could cause issues here (referring to JavaScript / jQuery and English language only) ?
function encodeEntities(value) {
return $('<div />').text(value).html();
}

JSON.parse not working for valid json [duplicate]

This question already has answers here:
json parse error with double quotes
(9 answers)
Closed 8 years ago.
I am using the below line in Javascript to parse a json string.
var obj = JSON.parse('{"respDataMap":{"userMessages":{"lbSearchHint":"Enteravalueandpress\"Enter\"orclickon\"Search\"#"}},"respErrorCode":"","respErrorMessage":""}');
The escaped double-quote character in the string is causing the json parsing to fail.
But, the same string pasted in online JSON validators is certified as valid.
How do I fix this?
Use double slash like for escape character
var obj = JSON.parse('{"respDataMap":{"userMessages":{"lbSearchHint":"Enteravalueandpress\\"Enter\\"orclickon\\"Search\\"#"}},"respErrorCode":"","respErrorMessage":""}');

Categories