Base64 is used for converting arbitrary binary data to ASCII text so, that it's become easier to transmitted the data over the network like e-mail and HTML form data.
It is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation (Source Wikipedia).
I will share, how you can achieve base64 encoding and decoding in C#, Java and Objective-C programming Languages.
So follow below code snippet for C#.
For Java See below code:
Note: Java 8 now supports BASE64 Encoding and Decoding. You can use the following classes:
For Objective C:
It is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation (Source Wikipedia).
I will share, how you can achieve base64 encoding and decoding in C#, Java and Objective-C programming Languages.
So follow below code snippet for C#.
Encode public static string Base64Encode(string plainText) { var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText); return System.Convert.ToBase64String(plainTextBytes); }
Decode public static string Base64Decode(string base64EncodedData) { var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData); return System.Text.Encoding.UTF8.GetString(base64EncodedBytes); }
For Java See below code:
Note: 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
Method 1 // 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); Method 2 import java.io.UnsupportedEncodingException; import javax.xml.bind.DatatypeConverter; public class EncodeString64 { public static void main(String[] args) throws UnsupportedEncodingException { String str = "77+9x6s="; // encode data using BASE64 String encoded = DatatypeConverter.printBase64Binary(str.getBytes()); System.out.println("encoded value is \t" + encoded); // Decode data String decoded = new String(DatatypeConverter.parseBase64Binary(encoded)); System.out.println("decoded value is \t" + decoded); System.out.println("original value is \t" + str); } }
For Objective C:
NSString *plainString = @"foo"; Encoding NSData *plainData = [plainString dataUsingEncoding:NSUTF8StringEncoding]; NSString *base64String = [plainData base64EncodedStringWithOptions:0]; NSLog(@"%@", base64String); // Zm9v Decoding NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0]; NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding]; NSLog(@"%@", decodedString); // foo
0 comments:
Post a Comment