Convert any string to Base64 in JavaScript - javascript

I am looking to take the contents of a file as a string (any file type), and convert that string into a string of Base64 solely using JavaScript for the purpose of outputting in a blob url. In many cases btoa() does not work, obviously due to it not accepting information that is not 8 bit. I need to be able to convert any string into a Base64 string. If this is possible, can someone please provide a solution?

Related

Decode Base64 string

I am currently working on a custom SAPUI5 app. I would like to make a service call, which expects the Guid in a different format than it is currently available.
An example:
Available (base64): QvLAUUzUCZbhAAAAjSS2iA==
As it should be (hexadecimal): 42F2C0514CD40996E10000008D24B688
I have not found an online decoder, which could produce the desired result, however, I was able to encode the guid 42F2C0514CD40996E10000008D24B to QvLAUUzUCZbhAAAAjss2iA== with the SAP ABAP standard function module HTTP_BASE64_ENCODE. With the usual online encoders, however, I got a different result.
How can I decode the encoded guid with JavaScript so that it has the desired format?
The string is in hexadecimal format you will have to convert it.
First you convert the string to binary (atob > charCodeAt) and then using the toString(16) you get the hex.
I will not post the code, since its already explained
Decode Base64 to Hexadecimal string with javascript

javascript string internal representation

As far as I know java uses UTF-16 to represent chars and string internally,
so if we load a text file from a file it is automatically decoded to its original encoding to utf-16.
Now the same can be said also for javascript
it also uses utf-16 as the internal string representation.
Suppose we load a string x encoded in utf-8 using ajax,
a converion takes place in order for javascript to be able to represent internally that string in UTF-16.
Please tell me if any of what I stated is correct or not,
because the real question is yet to come...
Now suppose the browser is rendering a page using utf-8 encoding,
and using javascript we want the browser to render also the ajax string x (as you normally do)
Would, in this case, a further conversion be needed from utf-16 to utf-8 ?
Thanks in advance.
According to this article, it is USC-2 or UTF-16

Javascript string compression for URL hash parameter

I'm looking to store a lot of data in a URL hash parameter without exceeding URL character limits.
Are there any conventional ways of compressing string length which could be then decoded on another page load?
I've seen LZW encoding used for similar solutions, however would special characters be valid for this use?
LZW encoding technically works; you'll just need to convert the LZW-encoded binary into URL-safe base64, so that the output doesn't contain special characters. Here's an MDN article on base64 in JavaScript; the URL-safe variant of base64 just replaces + with - and / with _. Of course, you're not likely to reduce the size of your string by much by doing this, unless the data you want to store is extremely compressible.
You can look at smaz or shoco, which are designed for the compression of short strings. Most compression methods don't really get rolling until well after your URL length limit, so you need a specialized compressor for this case if you expect to get any gain. You can then encode the binary result using a scheme like Base 64 or a more efficient coding that uses all of the URI-safe characters.

Convert a long text to a number (like hash, md5) that will get me same result in PHP and JS

I am trying to convert a small paragraph into a sequence of numbers (and maybe chars) like the md5 does.
I tried md5() in PHP and http://www.myersdaily.org/joseph/javascript/md5.js using JS but I get different result.
I do not know why this is happening, but can you suggest me a way to convert the text to a sequence of chars and numbers (to save them in DB) that will give me the same output? I do not mind if the output is not crypto.
Thank you
I would use the base64 encode/decode. Check out the link for the php http://php.net/manual/en/function.base64-encode.php and here is a link with some examples for javascript Base64 encoding and decoding in client-side Javascript
If you don't need crypto convert them into Hexadecimal value.
For example "Stack" will be 537461636B.
If you want to easily encrypt them, just use xor. This cannot give you different results in any possible language.
in PHP I found this function here PHP convert string to hex and hex to string
in JS I found some code here http://snipplr.com/view/52975/

How to convert an image string in javascript or jquery?

I have this "little" problem...
In my web app, I receive an xml message from the server that I parse with jquery. In the xml there is an element <Image> with these attributes: key and value. The value attribute is a string that represents the image file (.png) compressed with the gzip algorithm (using the System.IO.Compression class in C#). Is there a way to decompress the value in javascript and then get the corresponding decompressed string?
This looks like it's a combination of already answered questions. You need to ungzip the string and then convert from string to image. When dealing with problems like this it's best to break it down into the smallest chunks you can. That way your searches will yield more applicable results if you can't find your exact issue.
JavaScript implementation of Gzip
Javascript Hex String to Image
just simple example
<img alt="Embedded Image" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />
where iVBORw0KGgoAAAANSUhEUgAAADIA... - is your value..
I asume the image is base64 encoded. In that case you need to set the src property of the image with the image data prefixed with data:image/png;base64,
Embedding Base64 Images
The server send to me the string of the image in a gzip compress format. I try the tag but the browser seems not to decompress the image string. The image is converted in base64 format. Now I try again. Is there a way to check if the string is compress ed in the right way?

Categories

Resources