1 개요[ | ]
- base64_encode()
- b64encode()
- b64EncodeUnicode()
- Encodes string using MIME base64 algorithm
2 Bash[ | ]

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

Java
Copy
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));
}
Java
Copy
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[ | ]

순수 자바스크립트
JavaScript
Copy
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
JavaScript
Copy
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[ | ]
Objective-C
Copy
#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==
Objective-C
Copy
#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==
- Dependencies: utf8_encode, CHAR_AT, CHAR_CODE_AT
6 PHP[ | ]

PHP
Copy
$str = 'This is an encoded string';
echo base64_encode($str);
// VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==
PHP
Copy
echo base64_encode('★A가あ中'); // 4piFQeqwgOOBguS4rQ==
7 Python[ | ]
Python
Copy
import base64
str = 'This is an encoded string'
print base64.b64encode(str);
# VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==
Python
Copy
# -*- coding: utf-8 -*-
import base64
str = '★A가あ中'
print base64.b64encode(str);
# 4piFQeqwgOOBguS4rQ==
8 Perl[ | ]
Perl
Copy
use MIME::Base64 qw( encode_base64 );
my $str = 'This is an encoded string';
print encode_base64($str);
# VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==
9 같이 보기[ | ]
편집자 Jmnote Ykhwong Jmnote bot
로그인하시면 댓글을 쓸 수 있습니다.
리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― Jmnote리눅스 Python 2.7 컴파일 설치 ― ㅇㅇㅇ미운코딩새끼 ― 승호 도령미운코딩새끼 ― 불탄고등어미운코딩새끼 ― 김레이미운코딩새끼 ― 호박이미운코딩새끼 ― Junhg0211미운코딩새끼 ― 김왼손미운코딩새끼 ― 용딘이미운코딩새끼 ―Pinkcrimson
유기농냠냠파이썬 ― 호박유기농냠냠파이썬 ― 이에스유기농냠냠파이썬 ― 이승현파이썬 global ― Jmnote파이썬 global ― John Jeong파이썬 global ― Jmnote파이썬 global ― John Jeong파이썬 global ― John Jeong파이썬 global ― John Jeong파이썬 global ― Jmnote파이썬 global ― John Jeong