Posts

Showing posts with the label Base64

Base64 Encoder And Decoder

Answer : This is an example of how to use the Base64 class to encode and decode a simple String value. // String to be encoded with Base64 String text = "Test"; // Sending side byte[] data = null; try { data = text.getBytes("UTF-8"); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } String base64 = Base64.encodeToString(data, Base64.DEFAULT); // Receiving side byte[] data1 = Base64.decode(base64, Base64.DEFAULT); String text1 = null; try { text1 = new String(data1, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } This excerpt can be included in an Android activity. See android.util.Base64 It seems that this was added in API version 8 or android 2.2 so it will not be available on the older platforms. But the source of it is at android/util/Base64.java so if needed one could just copy it unchanged for older versions. Here is a simple method I was going to use until I realized that t...

Base64 Java Encode And Decode A String

Answer : You can use following approach: import org.apache.commons.codec.binary.Base64; // Encode data on your side using BASE64 byte[] bytesEncoded = Base64.encodeBase64(str.getBytes()); System.out.println("encoded value is " + new String(bytesEncoded)); // Decode data on other side, by processing encoded data byte[] valueDecoded = Base64.decodeBase64(bytesEncoded); System.out.println("Decoded value is " + new String(valueDecoded)); Hope this answers your doubt. Java 8 now supports BASE64 Encoding and Decoding. You can use the following classes: java.util.Base64 , java.util.Base64.Encoder and java.util.Base64.Decoder . Example usage: // encode with padding String encoded = Base64.getEncoder().encodeToString(someByteArray); // encode without padding String encoded = Base64.getEncoder().withoutPadding().encodeToString(someByteArray); // decode a String byte [] barr = Base64.getDecoder().decode(encoded); The accepted answer uses the Apache Commons p...

Check If A String Is Encoded In Base64 Using Python

Answer : I was looking for a solution to the same problem, then a very simple one just struck me in the head. All you need to do is decode, then re-encode. If the re-encoded string is equal to the encoded string, then it is base64 encoded. Here is the code: import base64 def isBase64(s): try: return base64.b64encode(base64.b64decode(s)) == s except Exception: return False That's it! Edit: Here's a version of the function that works with both the string and bytes objects in Python 3: import base64 def isBase64(sb): try: if isinstance(sb, str): # If there's any unicode here, an exception will be thrown and the function will return false sb_bytes = bytes(sb, 'ascii') elif isinstance(sb, bytes): sb_bytes = sb else: raise ValueError("Argument must be string or bytes") ...

Base64 Encode A Javascript Object

Answer : From String to Base-64 var obj = {a: 'a', b: 'b'}; var encoded = btoa(JSON.stringify(obj)) To decode back to actual var actual = JSON.parse(atob(encoded)) For reference look here. https://developer.mozilla.org/en/docs/Web/API/WindowBase64/Base64_encoding_and_decoding You misunderstood the Buffer(str, [encoding]) constructor, the encoding tells the constructor what encoding was used to create str , or what encoding the constructor should use to decode str into a byte array. Basically the Buffer class represents byte streams, it's only when you convert it from/to strings that encoding comes into context. You should instead use buffer.toString("base64") to get base-64 encoded of the buffer content. let objJsonStr = JSON.stringify(obj); let objJsonB64 = Buffer.from(objJsonStr).toString("base64");

Base64 Encoded Image Is Not Showing In Gmail

Answer : base64 encoded images are not well supported in email. They aren't supported in most web email clients (including Gmail) and are completely blocked in Outlook. Apple Mail is one of the few clients that does support them, that's why you're able to see them there but not elsewhere. Another thing to be mindful of with base64 encoded images is email file size. Gmail App (iOS, Android) and Outlook (iOS) truncate email messages whose file size exceeds 102KB. Remotely referenced images (Eg. <img src="http://www.website.com/image.png"> do not count towards the email's file size, but base64 encoded images do and can quickly blow out an email's file size past the 102KB limit. Just something else to consider. It looks like that direct encoded images (non-bease64 ) are also not supported by gmail :( - I write below snippet to convert image from base64 to direct form and send it in email - but still not see any image :( . To solve this issue ...