SOFTELメモ Developer's blog

会社概要 ブログ 調査依頼 採用情報 ...
てるてる動画

【php】base32_decode()

問題

phpで、base32_decode() したいです。

答え

function base32_decode($s)
{
	$m = array(
		"A" => 0, "B" => 1,
		"C" => 2, "D" => 3,
		"E" => 4, "F" => 5,
		"G" => 6, "H" => 7,
		"I" => 8, "J" => 9,
		"K" => 10, "L" => 11,
		"M" => 12, "N" => 13,
		"O" => 14, "P" => 15,
		"Q" => 16, "R" => 17,
		"S" => 18, "T" => 19,
		"U" => 20, "V" => 21,
		"W" => 22, "X" => 23,
		"Y" => 24, "Z" => 25,
		"2" => 26, "3" => 27,
		"4" => 28, "5" => 29,
		"6" => 30, "7" => 31,
	);

	$s = strtoupper($s);
	$l = strlen($s);
	$n = 0;
	$j = 0;
	$binary = "";
	for ($i = 0; $i < $l; ++$i) {
		$n = $n << 5;
		$n = $n + $m[$s[$i]];
		$j = $j + 5;

		if ($j >= 8) {
			$j = $j - 8;
			$binary .= chr(($n & (0xFF << $j)) >> $j);
		}
	}

	return $binary;
}

関連するメモ

コメント