I am having issues in turning Texture2D type image to bytes and then to string. When I do the following:
var myTextureBytes : byte[] = myTexture.EncodeToPNG();
Debug.Log(System.Text.Encoding.UTF8.GetString(myTextureBytes));
I just get a log output of "�PNG". Why is it so short? Whats the question mark? Shouldn't Unity be able to interpret UTF-8 chars? Also when I send that to my NodeJS server it says SyntaxError: Unexpected token and crashes the server.
the problem is that the bytes of PNG representation of the texture is not UTF-8 encoded, which is only for text.
To convert binary data to a string I would recommend base64 encoding.
var myTextureBytes : byte[] = myTexture.EncodeToPNG();
var myTextureBytesEncodedAsBase64 : String = System.Convert.ToBase64String(myTextureBytes);
have you tried using Default encoding?
Debug.Log(System.Text.Encoding.Default.GetString(myTextureBytes));
Related
I am receiving the content of a zip file (from an API) as a Base64-encoded string.
If I paste that string into Notepad++ and go
Plugins > MIME Tools > Base64 Decode
and save it as test.zip, it becomes a valid zip file, I can open it.
Now, I am trying to achieve the same thing in JavaScript.
I have tried atob(), and probably everything mentioned in the answers here and the code from Mozilla doc.
atob produces a similar content, but some characters are decoded differently (hence becomes an invalid zip file). The other methods throw an invalid URI error.
How can I reproduce Notepad++ behaviour in JavaScript?
The window.atob is only good for decoding data which fits in a UTF-8 string. Anything which cannot be represented in UTF-8 string will not be equal to its binary form when decoded. Javascript, at most, will try encoding the resultant bytes in to UTF-8 character sequence. This is the reason why your zip archive is rendered invalid eventually.
The moment you do the following:
var data = window.atob(encoded_data)
... you are having a different representation of your data in a UTF-8 string which is referenced by the variable data.
You should decode your binary data directly to an ArrayBuffer. And window.atob is not a good fit for this.
Here is a function which can convert base64 encoded data directly in to an ArrayBuffer.
As mentioned, do not use atob directly for decoding Base64 encoded zip files. You can use this function mentioned in https://stackoverflow.com/a/21797381/3508516 instead.
function _base64ToArrayBuffer(base64) {
var binary_string = window.atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
}
I want to pass a Base64 Image to the front end in a parameter.
I tried to send normal Base64 but it was giving me an error, probably because of the special characters in the Base64 Image.
So I tried in Java:
String base64Signature = Base64.getEncoder().encodeToString(image); // Encode to base64
return URLEncoder.encode(base64Signature, "utf-8"); // This class contains static methods for converting a String to the application/x-www-form-urlencoded MIMEformat
And in Javascript data.Signature has the image data. Neither
vm.Signature = data.Signature;
or
vm.Signature = decodeURIComponent(data.Signature);
worked. I copied the image data String in a online converter and it didn't display anything.
How should I do this?
The problem could be that Java's URLEncoder encodes spaces as + signs and JavaScript's decoder expects spaces as %20s. You can try replacing the + signs, for example:
decodeURIComponent(data.Signature.replace(/\+/g, '%20'));
Client side code (angularjs controller)
var p="gemüse8";
var pb= $base64.encode(p); // pb= Z2Vt/HNlOA==
from server side(C#)
string b64="Z2Vt/HNlOA==";
byte[] data = Convert.FromBase64String(b64);
string decodedString = Encoding.UTF8.GetString(data);
result showing "gem�se8"
How can i properly decode this string?
Whatever $base64.encode is seems to be using ISO-8859-1 (or similar) encoding rather than UTF - which probably needs addressing.
As it stands use:
string decodedString = Encoding.GetEncoding("iso-8859-1").GetString(data);
To communicate with a server, I need to send the password SHA1 & base64 encoded the same way CryptoJS does this.
My problem is that I'm using VB.NET. The typical base64 encoding (UTF-8) result is different than the results of CryptoJS.
How can I base64 encode the SHA1 string in .NET the same way CryptoJS encodes it?
You can see both results here: https://jsfiddle.net/hn5qqLo7/
var helloworld = "Hello World";
var helloword_sha1 = CryptoJS.SHA1(helloworld);
document.write("SHA1: " + helloword_sha1);
var helloword_base64 = helloword_sha1.toString(CryptoJS.enc.Base64);
document.write("1) Base64: " + helloword_base64);
document.write("2) Base64: " + base64_encode(helloword_sha1.toString()));
where base64_encode converts a given string to a Base 64 encoded string.
I saw a similar question, but I don't understand it.
Decode a Base64 String using CryptoJS
In (1) of your fiddle the CryptoJS calculates the SHA1 value of the string, and then, converts the raw bytes to Base64. However (2) first calculates the SHA1 value of 'Hello World' and then puts it in hexadecimal form (consisting of only 0-9 and a-f), and then converts this hexadecimal form of SHA1 to base64. So that is why you end up with two different results.
I have the following code:
var __dirname = '/home/ubuntu/Site/public/uploads/';
var base64Data = '/9j/2wBDABAICAgICBAICAgQEBAQECAYEBAQECggIBggMCgwMDAoMDAwOEhAMDhIODAwQFhASFBQUFBQMEBYYFhQYEhQUFD/2wBDARAQEBAQECgYGChQODA4UFBQUFBQUFBQUA==UFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUP/AABEIADwAUAMBIgACEQEDEQH/xAAfAAABBQEBAQEBAQAAAAAAAAAAAQIDBAUGBwgJCgv/xAC1EAACAQ==AwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eA==eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAw==BAUGBwgJCgv/xAC1EQACAQIEBAMEBwUEBAABAncAAQIDEQQFITEGEkFRB2FxEyIygQgUQpGhscEJIzNS8BVictEKFiQ04SXxFxgZGiYnKCkqNTY3ODk6Q0RFRkdISUpTVFVWVw==WFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/aAAwDAQACEQ==AxEAPwDmraLzIyTzkYINUby+njmEKtxH1HqeamkvXs7cxoPnPHPas4lyxZsknrUpGjZNJGsn7+2/4EvcUscjKMMKhViDkZqZJJnXaQSPcUwTAznOAKka2cJukxuPY06C3ELedA==oII+6PepA0074EW0E5x/n8qENu5Hb2zn5RkcGmeQ7zbCCMHmtKKMxuFHPBAPv6/zpJIgX4x+P+f85HpTTFYksFEafLxgdv8AP+fwp5k/doxYfLxz7f5J/P1pbYbUbn8cf59/1g==oYpMxMgP3T1/l/LNMogmt4rsHHUn+p/+tVY6W45DZHfj/PvU43xNkHIB/wA/y/WrEFwrDa/p/TH9Ki5NrlEWnlnLLx/F7f55/Kp7dEbnAwf8/wBTWi1vFOPl55/r/wDX/Wqktg==zQsWA6/eGev+fmoHYfbLHLHiQAsDwD74/wDZiPwBpXRcAx5x/D/T8hg/U1HauXmZF6nt/tf/AK2/SppkJTbGPvfd+nX+QX86ABVV2Mijj7q+4/8Arnim4+bcT+J/n/n1PpU6wA==dm0L7Y/p+v8A49TTCVO4n8f1z/X8TQMVflU9vr/n2x+HvWdJI8E5ZD9Rj/Pt+dX3YKMAdPX/AD+FULv7xYVQE5iOOR/niozCycjt/n+lS22oW8y4Zh0/oP8ACrIiikPB7/1NQA==FWG4kh4z/n/IFXkmiuVKsoOT3Hvj+oqBrX5dw9M/oDUW2WE/Ken/ANf/AAoAnayAkE9vJg+/Uf49f19qvaZZG/Mrx4jaJARHI3Xnt+QH4VRS5BHJ9v5/4ile7bAuIcEo3KH+JQ==PUfy/M0ATyh4hg8jGAPUf/q4+v0qB5SwLMR/j/n/ANmqeS8jZN5YYI6/1/kfxNZ9zdITiPgUDCabPGaqyupB5FJLN71VmmJ71SAhjWVT8oNXrO8uoiMgkA1TYlD8pNPjkc8ZpA==QjTXU224x2x+hFObUVY/Mnf+v/16zS7DvTSzY60Fl57xRjDAdP6f4U2K8BDKH49/pWc8jZojkbJ5osFy1Nc+SxMTkgnlaYb3PIFMVRK3zGnyWsUZUjPLYOTTsK5G1wzUxydhNQ==NNDHGDtFRFR5LH0xTE2f/9k=';
var buffer = new Buffer(base64Data, 'base64');
fs.writeFileSync(__dirname + 'zorro.jpg', buffer, 0, buffer.length);
However the saved image is corrupt and won't open in Finder. What am I doing wrong? Am I missing some header? The base64 string opens perfectly fine as inline data with an img tag.
EDIT: this works in HTML for me:
<img src="data:image/jpg;base64,/9j/2w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SppkJTbGPvfd+nX+
QX86ABVV2Mijj7q+4/8Arnim4+bcT+J/n/n1PpU6wHZtC+2P6fr/AOPU0wlTuJ/H9c/1/E0DFX5V
Pb6/59sfh71nSSPBOWQ/UY/z7fnV92CjAHT1/wA/hVC7+8WFUBOYjjkf54qMwsnI7f5/pUttqFvM
uGYdP6D/AAqyIopDwe/9TUAVYbiSHjP+f8gVeSaK5Uqyg5Pce+P6ioGtfl3D0z+gNRbZYT8p6f8A
1/8ACgCdrICQT28mD79R/j1/X2q9plkb8yvHiNokBEcjdee35AfhVFLkEcn2/n/iKV7tsC4hwSjc
of4lPUfy/M0ATyh4hg8jGAPUf/q4+v0qB5SwLMR/j/n/ANmqeS8jZN5YYI6/1/kfxNZ9zdITiPgU
DCabPGaqyupB5FJLN71VmmJ71SAhjWVT8oNXrO8uoiMgkA1TYlD8pNPjkc8ZpEI011NtuMdsfoRT
m1FWPzJ3/r/9es0uw700s2OtBZee8UYwwHT+n+FNivAQyh+Pf6VnPI2aI5GyeaLBctTXPksTE5IJ
5WmG9zyBTFUSt8xp8lrFGVIzy2Dk07CuRtcM1McnYTU00McYO0VEVHksfTFMTZ//2Q==
As to why the base64 encoded data is working on my end, the RFC4648 for base64 states this:
Furthermore, such specifications MAY ignore the pad character, "=",
treating it as non-alphabet data, if it is present before the end
of the encoded data. If more than the allowed number of pad
characters is found at the end of the string (e.g., a base 64
string terminated with "==="), the excess pad characters MAY also be
ignored.
Some implementation will ignore the added "=" and some will not.
EDIT:
As other has pointed out, your base64 string seems to be many base64 string concatenated together. Here is your string:
/9j/2wBDABAICAgICBAICAgQEBAQECAYEBAQECggIBggMCgwMDAoMDAwOEhAMDhIODAwQFhASFBQUFBQMEBYYFhQYEhQUFD/2wBDARAQEBAQECgYGChQODA4UFBQUFBQUFBQUA==
UFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUP/AABEIADwAUAMBIgACEQEDEQH/xAAfAAABBQEBAQEBAQAAAA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86ABVV2Mijj7q+4/8Arnim4+bcT+J/n/n1PpU6wA==
dm0L7Y/p+v8A49TTCVO4n8f1z/X8TQMVflU9vr/n2x+HvWdJI8E5ZD9Rj/Pt+dX3YKMAdPX/AD+FULv7xYVQE5iOOR/niozCycjt/n+lS22oW8y4Zh0/oP8ACrIiikPB7/1NQA==
FWG4kh4z/n/IFXkmiuVKsoOT3Hvj+oqBrX5dw9M/oDUW2WE/Ken/ANf/AAoAnayAkE9vJg+/Uf49f19qvaZZG/Mrx4jaJARHI3Xnt+QH4VRS5BHJ9v5/4ile7bAuIcEo3KH+JQ==
PUfy/M0ATyh4hg8jGAPUf/q4+v0qB5SwLMR/j/n/ANmqeS8jZN5YYI6/1/kfxNZ9zdITiPgUDCabPGaqyupB5FJLN71VmmJ71SAhjWVT8oNXrO8uoiMgkA1TYlD8pNPjkc8ZpA==
QjTXU224x2x+hFObUVY/Mnf+v/16zS7DvTSzY60Fl57xRjDAdP6f4U2K8BDKH49/pWc8jZojkbJ5osFy1Nc+SxMTkgnlaYb3PIFMVRK3zGnyWsUZUjPLYOTTsK5G1wzUxydhNQ==
NNDHGDtFRFR5LH0xTE2f/9k=
Notice how each of base64 string are 136 chracter long. If you decoded each of these base64 and append the result of each decoded base64 into a file, you will get your image.