함수 base64_encode()

(Base64 encode()에서 넘어옴)

1 개요[ | ]

base64_encode()
b64encode()
b64EncodeUnicode()
  • Encodes string using MIME base64 algorithm

2 Bash[ | ]

echo 'hello world' | base64
# aGVsbG8gd29ybGQK
str='hello world'
encoded=`echo $str | base64`
echo encoded=[$encoded]
# encoded=[aGVsbG8gd29ybGQK]
echo '★A가あ中' | base64
# 4piFQeqwgOOBguS4rQo=

3 Java[ | ]

private static String base64_encode(String str) {
	return new String(Base64.getEncoder().encode(str.getBytes()));
}
private static String base64_encode(byte[] bytes) {
	return new String(Base64.getEncoder().encode(bytes));
}
System.out.println( new String(Base64.getEncoder().encode("This is an encoded string".getBytes())) );
System.out.println( new String(Base64.getEncoder().encode("★A가あ中".getBytes())) );
// VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==
// 4piFQeqwgOOBguS4rQ==

4 JavaScript[ | ]

순수 자바스크립트
function base64_encode(str) {
  return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
    return String.fromCharCode('0x' + p1);
  }));
}
console.log( base64_encode('hello world') );
console.log( base64_encode('★A가あ中') );
 // aGVsbG8gd29ybGQ=
 // 4piFQeqwgOOBguS4rQ==
CrytoJS
console.log( CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse('hello world')) );
console.log( CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse('★A가あ中')) );
// aGVsbG8gd29ybGQ=
// 4piFQeqwgOOBguS4rQ==

5 Objective-C[ | ]

#define CHAR_AT(str, n) [str substringWithRange:NSMakeRange(n,1)]
#define CHAR_CODE_AT(str, n) (int)[str characterAtIndex:n]
+(NSString*) base64_encode:(NSString*)str {
    NSString* key = @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    NSString* output = @"";
    int c1, c2, c3, e1, e2, e3, e4;
    int i = 0;
    str = [Util utf8_encode:str];
    while (i < [str length]) {
        c2 = c3 = -1;
        c1 = CHAR_CODE_AT(str, i++);
        if(i<[str length]) c2 = CHAR_CODE_AT(str, i++);
        if(i<[str length]) c3 = CHAR_CODE_AT(str, i++);
        e1 = c1 >> 2;
        e2 = (c1&3) << 4;
        if(c2 < 0) e3 = e4 = 64;
        else {
            e2 = e2 | (c2>>4); e3 = (c2&15) << 2;
            if(c3 < 0) e4 = 64;
            else { e3 = e3 | (c3>>6); e4 = c3 & 63; }
        }
        output = [Util concat:output,CHAR_AT(key,e1),CHAR_AT(key,e2),CHAR_AT(key,e3),CHAR_AT(key,e4),nil]; 
    }
    return output;
}
NSLog(@"%@", [Util base64_encode:@"★A가あ中"]); // 4piFQeqwgOOBguS4rQ==
#define CHAR_AT(str, n) [str substringWithRange:NSMakeRange(n,1)]
#define CHAR_CODE_AT(str, n) (int)[str characterAtIndex:n]
+(NSString*) base64_encode:(NSString*)str {
    NSString* key = @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    NSString* output = @"";
    int c1, c2, c3, e1, e2, e3, e4;
    int i = 0;
    str = [Util utf8_encode:str];
    while (i < [str length]) {
        c2 = c3 = 0;
        c1 = CHAR_CODE_AT(str, i++);
        if(i<[str length]) c2 = CHAR_CODE_AT(str, i++);
        if(i<[str length]) c3 = CHAR_CODE_AT(str, i++);
        e1 = c1 >> 2;
        e2 = (c1&3) << 4 | c2>>4;
        e3 = (c2&15) << 2 | c3>>6;
        e4 = c3 & 63;
        if(c2 < 1) e3 = e4 = 64;
        if(c3 < 1) e4 = 64;
        output = [Util concat:output,CHAR_AT(key,e1),CHAR_AT(key,e2),CHAR_AT(key,e3),CHAR_AT(key,e4),nil]; 
    }
    return output;
}
NSLog(@"%@", [Util base64_encode:@"★A가あ中"]); // 4piFQeqwgOOBguS4rQ==

6 PHP[ | ]

$str = 'This is an encoded string';
echo base64_encode($str);
// VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==
echo base64_encode('★A가あ中'); // 4piFQeqwgOOBguS4rQ==

7 Python[ | ]

import base64
str = 'This is an encoded string'
print base64.b64encode(str);
# VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==
# -*- coding: utf-8 -*-
import base64
str = '★A가あ中'
print base64.b64encode(str);
# 4piFQeqwgOOBguS4rQ==

8 Perl[ | ]

use MIME::Base64 qw( encode_base64 );
my $str = 'This is an encoded string';
print encode_base64($str);
# VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==

9 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}