I have string that is encoded in UTF16 and i want to decode it using JS, when i use simple decodeURI()
function i get the desired result but in case when special characters are there in the string like á, ó, etc it do not decodes.
On more analysis i came to know that these characters in the encoded string contains the ASCII value.
Say I have string "Acesse já, Encoded version : "Acesse%20j%E1". How can i get the string from the encode version using java script?
EDIT:
The string is a part of URL
Ok, your string seems to have been encoded using escape, use unescape to decode it!
unescape('Acesse%20j%E1'); // => 'Acesse já'
However, escape and unescape are deprecated, you’d better use encodeURI or encodeURIComponent here.
encodeURIComponent('Acesse já'); // => 'Acesse%20j%C3%A1'
decodeURIComponent('Acesse%20j%C3%A1'); // => 'Acesse já'
Related
if I escape '가온' in javascript,
the result would be:
escape('가온')
'%uAC00%uC628'
I want to get the same result like the js in python.
However, if I encode as ascii like:
byte_string= "누리".encode('ascii', 'backslashreplace')
b'\\ub204\\ub9ac'
the result isn't same. How can I?
Something is confusing in your question: first you encode '가온' then '누리'.
I think encoding with 'backslashreplace' can work as long as you decode and replace the backslashes with %:
"가온".encode('ascii', 'backslashreplace').decode().replace('\\', '%')
Output:
%uac00%uc628
JavaScript escape() is URL escape.
You can get this using urllib in Python.
Note that URLs may have multiple different encodings for the same characters. However any valid URL parses should accept any encoding.
After getting byte array encryptedMessageInBytes from AES encryption function call cipher.doFinal in Java, I convert the byte array to base64 like this:
String encryptedMessageInBase64 = new String(Base64.getEncoder().encode(encryptedMessageInBytes));
In JavaScript, I simply do .toString() to the output I get from CryptoJS.AES.encrypt method and I get exact same base64 string. i.e.
var encryptedMessageInBase64 = CryptoJS.AES.encrypt("Message", "Secret Passphrase").toString();
It gives same base64 string as in Java code.
However, in one of the Java source code, they have done like this:
String encryptedMessageInBase64 = Base64.getUrlEncoder().encodeToString(encryptedMessageInBytes);
What shall I do in JavaScript to obtain same base64 string?
Here is answer:
However, in one of the Java source code, they have done like this:
String encryptedMessageInBase64 = Base64.getUrlEncoder().encodeToString(encryptedMessageInBytes);*
Here, basically they have done UrlEncoding instead of Base64 encoding. It is nothing but replacing + and / characters with - and _ characters. Url encoding is when we want to send encoded string over HTTP where it treats these two as special characters this is why we need to replace them with some other characters which are HTTP friendly.
I'm working to integrate some code with a third party, and sometimes a string argument they pass to a Javascript function I'm writing will be encoded using encodeURIComponent, sometimes it won't be.
Is there a definitive way to check whether it's been encoded using encodeURIComponent
If not, I'll do the encoding then
You could decode it and see if the string is still the same
decodeURIComponent(string) === string
Not reliably, especially in the case where a string may be encoded twice:
encodeURIComponent('http://stackoverflow.com/')
// yields 'http%3A%2F%2Fstackoverflow.com%2F'
encodeURIComponent(encodeURIComponent('http://stackoverflow.com/'))
// yields 'http%253A%252F%252Fstackoverflow.com%252F'
In essence, if you were to try and detect the string encoding when the passed argument is not actually encoded but has qualities of an encoded string, you'd be decoding something you shouldn't.
I'd recommend adding a second parameter in the definition "isURIComponent".
However, if you wanted to attempt, perhaps the following would do the trick:
if ( str.match(/[_\.!~*'()-]/) && str.match(/%[0-9a-f]{2}/i) ) {
// probably encoded with encodeURIComponent
}
This tests that the non alphanumeric characters that don't get encoded are intact, and that hexadecimals exist (e.g. %20 for a space)
I encoded a string using js method:
var result = escape('Вася')
and get as result: "%u0412%u0430%u0441%u044F"
how can I decode this string in Java?
This doesn't work:
URLDecoder.decode(text, "UTF-8");
Use encodeURI(), which more-less is equivalent to URLEncoder.encode() which in turn happen to be the reverse of URLDecoder.decode(). You may also try with encodeURIComponent() which handles non-ASCII characters more graciously.
I got a json structure somehow as below and my question is how can i parse this with jQuery so that i can use it like myJson[0].name and than alert it so that "M\\xe9t\\xe9o" = Météo.
Jquery tells me this is invalid json why ?
Json uses double backslash if i use single backslash ("M\xe9t\xe9o") Jquery is OK with the syntax.
var jsonObj = '{"title":[{"id":"1","name": "M\\xe9t\\xe9o"},{"id":"2","name": "Meteo"}]}';
var myJson = jQuery.parseJSON(jsonObj);
The JSON syntax only allows \uxxxx escapes.
Change it to "M\\u00e9t\\u00e9o".
If you use a single backslash, it gets parsed by the Javascript string literal, so the actual string value contains the real Unicode character, not an escape. In other words, "M\xe9t\xe9o" === "Météo"
It is looks like the json was incorrectly (manually?) encoded. When you encode it in UTF-8, e.g. with PHP, you'll get:
{"title":[{"id":"1","name": "M\u00e9t\u00e9o"},{"id":"2","name": "Meteo"}]}
which is correctly parsed by JS. But \xe9 is unrecognized by parser.