"함수 dec to base62()"의 두 판 사이의 차이

30번째 줄: 30번째 줄:
$str = '';
$str = '';
while( $num > 0 ) {
while( $num > 0 ) {
$str = $chars[$q % 62] . $str;
$str = $chars[$num % 62] . $str;
$num = floor( $num / 62 );
$num = floor( $num / 62 );
}
}

2016년 4월 4일 (월) 20:05 판

1 JavaScript

function dec_to_base62(num) {
	if (num === 0) return '0';
	var chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var s = '';
	while (num > 0) {
		s = chars[num % 62] + s;
		num = Math.floor(num/62);
	}
	return s;
}
console.log( dec_to_base62(0) );
console.log( dec_to_base62(999) );
console.log( dec_to_base62(1234) );
console.log( dec_to_base62(12345) );
// 0
// g7
// jU
// 3d7

2 PHP

function dec_to_base62( $num ) {
	if ( $num == 0 ) return '0';
	$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	$str = '';
	while( $num > 0 ) {
		$str = $chars[$num % 62] . $str;
		$num = floor( $num / 62 );
	}
	return $str;
}
var_dump( dec_to_base62( 0 ) );
var_dump( dec_to_base62( 999 ) );
var_dump( dec_to_base62( 1234 ) );
var_dump( dec_to_base62( 12345 ) );
# string(1) "0"
# string(2) "g7"
# string(2) "jU"
# string(3) "3d7"

3 Python

def dec_to_base62(x):
	digits = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
	base62 = []
	while(True):
		rem = x % 62
		base62.insert(0, digits[rem])
		x = int(x) / 62
		if(x <= 0):
			break
	return(''.join(base62))

print dec_to_base62(0)
print dec_to_base62(999)
print dec_to_base62(1234)
print dec_to_base62(12345)
# 0
# g7
# jU
# 3d7

4 같이 보기

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