Perl

10進数から2進数

	#!/usr/bin/perl
	use CGI;
	my $charset="utf-8";
	my $title="小樽商科大学";
	my $myself = './From10To2.cgi';#<------------------修正
	my $q = new CGI;
	print $q->header(-charset=>$charset);
	print $q->start_html(-title=>$title, -encoding=>$charset);
	print $q->h1('10進数から2進数への変換');
	print $q->h3('半角数字で10進数を入力して下さい。');
	#時間
	my @time = localtime(time);
	$time[4]++;
	$time[5] += 1900;
	#print "<br>$time[5]月$time[4]月$time[3]日$time[2]時$time[1]分$time[0]秒<br>";
	#print "$ENV{'REMOTE_ADDR'}<br>";
	#入力単語
	if($q->param('user')){
	
	        if($q->param('user') =~ /^\d+$/){
	                print "入力(10進数):" . $q->param('user') . "<br>";
	                # 10進数から2進数
	                my $bin = sprintf "%b", $q->param('user');
	                print "結果(2進数)" . $bin, "<br>\n"; # 1111101000
	                # 2進数から10進数
	                #my $dec = oct "0b" . $q->param('user');
	                #print "結果(10進数):" .  $dec, "<br>"; # 1000
	        }
	        else{
	                print  "「" . $q->param('user')  . "」は10進数ではありません" . "<br>";
	        }
	}
	print $q->start_form(-method => 'POST', -action => $myself);
	print qq|<input type="text" size="30" name="user" value="13">|;
	print qq|<input type="submit" name="submit" value="送信">|;
	print $q->end_form();
	print $q->end_html();
	}