get raw utf8 character strings - javascript

I am saving a string to a data tag in html to be used by javascript and sent to a php API. I am working with a string that looks like:
\n\u003\u0010\u0001\u0018
After I save it to the data variable, it looks like:
↵0
which is fine. The original string still appears to be intact if you do a decodeURLComponent on it.
What I'd like to do is either decode the symbols to the original string in PHP or javascript because it needs to be consumed as the original string, but I'm struggling in both spots to get it done. Thoughts?

You can use Base64 to encode and decode string.
There are some example for javascript on the documentation.
And PHP's documentation for encode and decode..

Related

json_encode and fwrite of php not able to recognize binary data why?

json_encode of php and fwrite of php not able to recognize binary data why ?
When i am taking some image in binary format then trying to convert in json format when calling in ajax from javascript/jquery side, it is showing null. Again when trying to write the data in a file via fwrite() of php then also it has written null only.
I solved this issue using base64 encode format. The base64 format can be writable to file as well as taken to json encode format too.
May be json_encode() and fwrite() of php is for texts only.
From what i understand, you want to send binary data using json. One thing to keep in mind is that the data is always transferred as string. In order to send binary data, convert the binary number into string and then json_encode it as
{
"array": [
010101000,
100010001,
010001001
]
}
The binary number shown above is actually string. On client side, you have to interpret this string as binary.
If you want to use ajax call to fetch data from server, you should use base64 encoding rather than binary.

sending Json from JavaScript to codebehind

Output function javascript: (use json.stringify)
"[\"Lighthouse(1).jpg\",\"Penguins(2).jpg\"]"
how convert this output to json in c#.
Is there a function to convert to Json
tanks for answer
There is no problem here, the output data you have shown in the hidden field is in fact valid JSON. Square brackets are used to denote an array of data which is why it is showing here as you are converting a javascript array into JSON. Curly braces which is what you may be thinking of for JSON is used to denote objects in JSON. You can see more on JSON at the w3schools page.
I have tested your JSON string that you have shown in a few JSON decoders for different languages and all convert the data to an array in the native language without any errors.

How do I manipulate the following string so that json_decode() can accept it?

This is my problem:
The user provides the following string in a text input element(It's a url to a location on google maps):
https://www.google.com/maps/place/40%C2%B048'45.7%22N+73%C2%B036'22.2%22W/#40.8130626,-73.6039651,15z/data=!4m2!3m1!1s0x0:0x0?hl=en
I take the string value above via jquery along with some other text inputs, store it in an object, convert the values into a JSON String, and post it to the server for processing via ajax.
Once the server receives it(I use php on the server side), I take the JSON string and provide it as an argument for json_decode(). json_decode() returns null indicating that there was an error in the formatting of the JSON string it received. I know the part of the JSON string that was at fault was the one I listed above because my script works if it's not included.
My question is. How do I manipulate the string above to make it work with json_decode()? JSONLint.com tells me that the string above is valid JSON. I've looked at these following posts and tried out suggested solutions such as utf8_encode() and bin2hex() to clean up the string;however, json_decode() still returns null:
json_decode returns NULL after webservice call
json_decode() returns null issues
I now know that there are encoding issues; however, I'm at a loss with regards to how to make the string above compatible with json_decode(). I don't post on SO unless I'm really stuck with an issue, so any help is greatly appreciated.

Salesforce API insert adds special characters

I am using salesforce PHP toolkit in order to insert values of javascript functions (Just in order to document functions I am using, not for execution in salesforce) inside a custom object I have.
In my PHP function I am saving a string like:
(function(d,f){var b={src:(d.location.protocol=="https:"?"https:":"http:")...
after I insert this string using SF API, The result I see in the field is:
(function(d,f){var b={src:(d.location.protocol=="https:"?"...
As you can see, salesforce has added special characters to my string.
I haven't found anyway to pass that.
Any idea's?
The solution was to remove the htmlspecialchars from the string. I didn't think that SF would accept the string without because you could not echo the string without it as well (Due to special characters in my string). But it seems that it does pass the parameter without any issue. I'd be happy to understand that if anyone understands.

encode string as utf-16 to base64 in javascript

I'm struggling to find any resources on this online, which is concerning.
I've been reading about UCS-2 and UTF-16 woes, but I can't find a solution.
I need to get a value from an input:
var val = $('input').val()
and encode it to base64, treating the text as utf-16, so:
this is a test
becomes:
dABoAGkAcwAgAGkAcwAgAGEAIAB0AGUAcwB0AA==
and not the below, which you get treating it as UTF-8:
dGhpcyBpcyBhIHRlc3Q=
Your data, once read into JavaScript, will be in an encodingless numerical format (strictly speaking, it has to be in Unicode Normalised Form C, but Unicode is just a series of identifying numbers for each glyph in the Unicode lexicon. It's encoding-less). So: if you specifically need the data encoded as a UTF-16 byte sequence, do so, then base64 encode that.
But here's the fun part: which UTF-16 do you need? Little or Big Endian? With or without BOM? UTF-16 is a really inconvenient encoding format (we're not even going to touch UCS-2. It's obsolete. Has been for a long time).
What you really should need is to get a text value from your HTML element, Base64 encode its value, and then have whatever receives that data unpack it as UTF8; don't try to make JavaScript do more work than it has to. I presume you're sending this data to a server or something, in which case: your server language is way more elaborate than JavaScript, and can unpack text in about a million different encodings thanks to built-in functions. So just use that. Don't solve Y for X.

Categories