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.
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 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á'
I have the following variable:
var string="Mazatl%E1n";
The string is returned like that by the server. All I want to do is decode that into: Mazatlán. I've tried the following:
var string="Mazatl%E1n";
alert(unescape(string));
alert(decodeURI(string));
unescape works fine but I don't want to use it because I understand it is deprecated, instead I tried decodeURI which fails with the following error:
Uncaught URIError: URI malformed
Why ? Any help is appreciated.
var string="Mazatl%E1n";
alert(unescape(string));
alert(decodeURI(string));
You get the error because %E1 is the Unicode encoding, but decodeURI() expects UTF-8.
You'll either have to create your own unescape function, for example:
function unicodeUnEscape(string) {
return string.replace(/%u([\dA-Z]{4})|%([\dA-Z]{2})/g, function(_, m1, m2) {
return String.fromCharCode(parseInt("0x" + (m1 || m2)));
})
}
var string = "Mazatl%E1n";
document.body.innerHTML = unicodeUnEscape(string);
or you could change the server to send the string encoded in UTF-8 instead, in which case you can use decodeURI()
var string = "Mazatl%C3%A1n"
document.body.innerHTML = decodeURI(string);
URI supports the ASCII character-set , and the correct format encoding for á is %C3%A1 (in UTF-8 encoding)
fiddle
escape and unescape use an hexadecimal escape sequences
(which is different ..);
so the value you're getting form the server has been encoded using escape(string).
The decodeURI() function expects a valid URI as its parameter. If you are only trying to decode a string instead of a full URI, use decodeURIComponent()
Is there a javascript function that takes a string and converts it into another string that is percent-encoded? That way something like "This Guy" turns into "This%20Guy".
Thanks
encodeURI, encodeURIComponent or escape will work the same way for your string, but they differ in details.
encodeURI is just for escaping URLs
encodeURIComponent also escapes = and &
escape works differently with non-ASCII unicode symbols
encodeURI("Ω") === encodeURIComponent("Ω") === "%CE%A9"
escape("Ω") === "%u03A9"
if you need to send a string as part of request, use encodeURIComponent
Try encodeURIComponent() or escape()
Try this encodeURIComponent()
var stringToDecode = "J&K";
var encodedString = encodeURIComponent(stringToDecode );
Use decodeURIComponent() to decode it again when needed
More Info here
https://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
Yes, here is
escape('This Guy');