📖
10진수 < - > 16진수 변환기 PHP소스
페이지 정보
본문
10진수와 16진수 간의 변환을 수행하는 코드를 포함한 전체 웹 페이지 소스코드입니다. 이 코드는 간단한 HTML 폼과 CSS로 디자인되어 있으며, 입력된 10진수 숫자를 16진수로 변환하거나, 입력된 16진수 숫자를 10진수로 변환할 수 있습니다.
[code]<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>Decimal <-> Hexadecimal Converter</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f5f5f5;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
width: 300px;
text-align: center;
}
h2 {
margin-bottom: 20px;
color: #333;
}
input[type="text"] {
width: 80%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
input[type="submit"] {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.result {
margin-top: 20px;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<h2>10진수 <-> 16진수 변환기</h2>
<form method="post">
<input type="text" name="decimal" placeholder="10진수를 입력하세요" value="[code]<?php echo isset($_POST['decimal']) ? htmlspecialchars($_POST['decimal']) : ''; ?>[/code]">
<input type="submit" name="to_hex" value="10진수 -> 16진수">
<br>
<input type="text" name="hex" placeholder="16진수를 입력하세요" value="[code]<?php echo isset($_POST['hex']) ? htmlspecialchars($_POST['hex']) : ''; ?>[/code]">
<input type="submit" name="to_decimal" value="16진수 -> 10진수">
</form>
<div class="result">
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['to_hex']) && !empty($_POST['decimal'])) {
$decimal = $_POST['decimal'];
if (is_numeric($decimal)) {
$hex = dechex($decimal);
echo "10진수 {$decimal}는 16진수로 {$hex} 입니다.";
} else {
echo "유효한 10진수를 입력하세요.";
}
} elseif (isset($_POST['to_decimal']) && !empty($_POST['hex'])) {
$hex = $_POST['hex'];
if (ctype_xdigit($hex)) {
$decimal = hexdec($hex);
echo "16진수 {$hex}는 10진수로 {$decimal} 입니다.";
} else {
echo "유효한 16진수를 입력하세요.";
}
}
}
?>
</div>
</div>
</body>
</html>[/code]
$_SERVER['REQUEST_METHOD']를 사용하여 폼이 제출되었는지 확인합니다.
is_numeric() 함수를 사용하여 10진수 입력이 유효한 숫자인지 확인합니다.
ctype_xdigit() 함수를 사용하여 16진수 입력이 유효한 16진수인지 확인합니다.
dechex() 함수를 사용하여 10진수를 16진수로 변환하고, hexdec() 함수를 사용하여 16진수를 10진수로 변환합니다.
[code]<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>Decimal <-> Hexadecimal Converter</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f5f5f5;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
width: 300px;
text-align: center;
}
h2 {
margin-bottom: 20px;
color: #333;
}
input[type="text"] {
width: 80%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
input[type="submit"] {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.result {
margin-top: 20px;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<h2>10진수 <-> 16진수 변환기</h2>
<form method="post">
<input type="text" name="decimal" placeholder="10진수를 입력하세요" value="[code]<?php echo isset($_POST['decimal']) ? htmlspecialchars($_POST['decimal']) : ''; ?>[/code]">
<input type="submit" name="to_hex" value="10진수 -> 16진수">
<br>
<input type="text" name="hex" placeholder="16진수를 입력하세요" value="[code]<?php echo isset($_POST['hex']) ? htmlspecialchars($_POST['hex']) : ''; ?>[/code]">
<input type="submit" name="to_decimal" value="16진수 -> 10진수">
</form>
<div class="result">
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['to_hex']) && !empty($_POST['decimal'])) {
$decimal = $_POST['decimal'];
if (is_numeric($decimal)) {
$hex = dechex($decimal);
echo "10진수 {$decimal}는 16진수로 {$hex} 입니다.";
} else {
echo "유효한 10진수를 입력하세요.";
}
} elseif (isset($_POST['to_decimal']) && !empty($_POST['hex'])) {
$hex = $_POST['hex'];
if (ctype_xdigit($hex)) {
$decimal = hexdec($hex);
echo "16진수 {$hex}는 10진수로 {$decimal} 입니다.";
} else {
echo "유효한 16진수를 입력하세요.";
}
}
}
?>
</div>
</div>
</body>
</html>[/code]
$_SERVER['REQUEST_METHOD']를 사용하여 폼이 제출되었는지 확인합니다.
is_numeric() 함수를 사용하여 10진수 입력이 유효한 숫자인지 확인합니다.
ctype_xdigit() 함수를 사용하여 16진수 입력이 유효한 16진수인지 확인합니다.
dechex() 함수를 사용하여 10진수를 16진수로 변환하고, hexdec() 함수를 사용하여 16진수를 10진수로 변환합니다.
댓글목록
등록된 댓글이 없습니다.
![]() ![]() |