<?php
	class NumChar {
		public $input, $output, $msg;
		private array $aryR;
		private int $siz;

		public function __construct(string $input = "") {
			$this->input = $input;
			$this->aryR = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
				, "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "m", "n", "o", "p", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
			$this->siz = count($this->aryR);
		}

		public function chgNum(): bool {
			if (!preg_match("/^[A-Za-z0-9]+$/", $this->input)) {
				$this->msg = "格式錯誤!" . $this->input . "!";
				return false;
			}
	
			$aryI = str_split($this->input);
			$this->output = 0;
	
			foreach (array_reverse($aryI) as $char) {
				$num1 = array_search($char, $this->aryR, true);
				$this->output = $this->output * $this->siz + $num1;
			}
	
			return true;
		}

		public function chgChar(): bool {
			if (!preg_match("/^\d+$/", $this->input)) {
				$this->msg = "格式錯誤!" . $this->input . "!";
				return false;
			}
	
			$this->output = "";
			$this->input = (int)$this->input;
	
			while ($this->input > $this->siz) {
				$this->output .= $this->aryR[$this->input % $this->siz];
				$this->input = intdiv($this->input, $this->siz);
			}
	
			$this->output .= $this->aryR[$this->input];
			return true;
		}
	}
?>