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

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
1번째 줄: 1번째 줄:
==JavaScript==
==JavaScript==
[[category:JavaScript]]
[[category:JavaScript]]
<source lang='JavaScript'>
<syntaxhighlight lang='JavaScript'>
function dec_to_base62(num) {
function dec_to_base62(num) {
if (num === 0) return '0';
if (num === 0) return '0';
20번째 줄: 20번째 줄:
// jU
// jU
// 3d7
// 3d7
</source>
</syntaxhighlight>


==PHP==
==PHP==
[[category: PHP]]
[[category: PHP]]
<source lang='php'>
<syntaxhighlight lang='php'>
function dec_to_base62( $num ) {
function dec_to_base62( $num ) {
if ( $num == 0 ) return '0';
if ( $num == 0 ) return '0';
43번째 줄: 43번째 줄:
# string(2) "jU"
# string(2) "jU"
# string(3) "3d7"
# string(3) "3d7"
</source>
</syntaxhighlight>


==Python==
==Python==
[[category: Python]]
[[category: Python]]
<source lang='Python'>
<syntaxhighlight lang='Python'>
def dec_to_base62(x):
def dec_to_base62(x):
digits = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
digits = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
67번째 줄: 67번째 줄:
# jU
# jU
# 3d7
# 3d7
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

2020년 11월 2일 (월) 02:32 기준 최신판

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 }}