Convert Int16 array to base64 string in Javascript - javascript

I need to send audio data to an API which requires a base64 string as input.
Therefore I'm looking for a way to convert an Int16Array to a base64 string. Is there a simple procedure to do that? All methods that I've found so far applied to uint8 data...

Related

Decoding a Java serialized UUID object in Javascript

We have a Java UUID being written to a ByteArrayOutputStream. Then bytearray of that ByteArrayOutputStream is base64 encoded and sent in a JSON. The encoding process is as follows.
public String uuidToString(UUID uuidObj) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final DataOutputStream dos = new DataOutputStream(baos);
dos.writeLong(uuidObj.getMostSignificantBits());
dos.writeLong(uuidObj.getLeastSignificantBits());
dos.flush();
return Base64.encodeBase64URLSafeString(baos.toByteArray());
}
I am trying to figure out how to decode this String in the javascript side. Ofcourse the base64 part is easy. Using atob I get a String and then using Crypto.js I can get a byte array out of that base64 decoded String. Then I tried to use the conversion techniques mentioned in the following link to convert but its always returning garbage.
Converting byte array to string in javascript
Using the String as is directly in JSON is not an option for a couple of reasons. Firstly we want to keep this String opaque, but we also want to keep the size of the final encoded string that is sent over the wire to be small.
I know another option is protocol buffers, but the lack of a supported js implementation and the extra build time leg work seem to be barriers on that front.
So do I have any other options ? And any suggestions on why my javascript decoding isn't working.

Sending BLOB to APEX RESTful service

I want to send a BLOB to an APEX RESTful service. The payload has to be a JSON (not form-data, I've had a lor of problems with that in the server side).
The BLOB is an image that I need to upload to the DB, my problem is that I don't know how to send the data in the payload. Should it be a string that represents the array of bytes? Should it be the array itself? (I managed to extract a binary string with the readASBinaryString method of javascipt's FileReader).
Thanks for any help.
Your BLOB data could be encoded to Base64 and then transported in JSON document as a string. You can use window.btoa() to convert binary data to Base64 string.
For example:
{
"data": "SGVsbG8gd29ybGQ="
}

Parsing physical new lines present in a string in Javascript/Node for storage

I am submitting data to the server using UTF-8 and encoding all entries being sent to the server. The objects I am sending are parameters with accompanying JSON data that partly consists of special characters.
For example, for the description field of the JSON data, the actual input into a textarea is:
!##$%^&*()
""''
this is a new line
The UTF-8 encoded data being sent to the server looks like:
&line={"description":"!%40%23%24%25%5E%26*()%0A%22%22''%0A%0Athis%20is%20a%20new%20line"}
with the parameter being 'line'.
On the server side, I am trying to parse the line parameter, but it has been decoded back into a readable string, with the special characters and new lines looking exactly like they did in the input data, albeit as part of JSON. When I try to parse this string into JSON, which now looks like:
{"description":"!##$%^&*()
""''
this is a new line"}
I get the 'SyntaxError: Unexpected token' error, which is expected assuming that it is caused by the breaks in the string. How would I rectify this issue? I have tried re-encoding the string using encodeURIComponent, but that encodes literally every element of the string.
I will ultimately be saving this data as part of a document in a NoSQL solution; the parsing is being done as part of a validation procedure to ensure entry data is of proper JSON format.

encode/decode Query String ReturnUrl Parameter

I am sending the return url as a query string parameter like this http://localhost:50316/TripNestFinal/Login.aspx?ReturnUrl=~/Account/AccountSettings.aspx
But i want ~/Account/AccountSettings.aspx to be encoded in such a way so that i can
encode/decode in jQuery/javascript
encode/decode in VB.NET
encode in VB.NET and decode in javascript/jQuery and vice versa
This doesn't have to be bulletproof as i am not dealing with security here. All i want to do is to change this ~/Account/AccountSettings.aspx to something that does not show the path directly.
I thought of using Base64 encoding but when i Base64 Encode , it includes a '/' character which breaks my jQuery logic. Is there any way i can avoid the '/' character when i Base64 encode?
You will need to URL encode the Base64 encoded string, that will work - although you'll need to check the generated query string length to make sure it doesn't hit the .NET maximum.
But I have to ask why you'd want to encode it in this way?

javascript chinese/japanese character decoding

I created a JSONP function on the server and returns a UTF-8 encoded json object like this
applyLocalization({"Name":"%E5%90%8D%E5%89%8D","Age":"%E5%B9%B4%E9%BD%A2"});
on my javascript on the client side, i want to convert the garbled part to their original state like
{"Name":"名前", "Age":"年齢"}
I tried $.parseJSON() but it doesnt work
You can use decodeURIComponent to decode urlencoded strings like yours
decodeURIComponent('%E5%90%8D%E5%89%8D');
//result: '名前'
You could use the decodeURIComponent function. But you shouldn't be URL encoding your javascript strings. You should send them as UTF-8 strings as-is. Javascript is capable of understanding them.

Categories

Resources