โดยทั่วไปแล้วเราสามารถใช้
function dechex() แปลงเลขฐาน 10 เป็นเลขฐาน 16ได้เลย
แต่นี่มันตัวอย่างวิธีเขียนแบบธรรมดา
โคตค่อนข้างยาวหน่อย เหมาะสำหรับคนหัดศึกษา
โค้ด: เลือกทั้งหมด
<?php
$input = "234";
//ใส่ตัวเลขที่ต้องการแปรรหัส
if($input < 0 or $input > 256){
echo "กรุณากรอกตัวเลข [ 0 -255 ] ";
}
else if ($input <=15){
switch($input){
case "0":
echo 00;
break;
case"1":
echo 01;
break;
case"2":
echo 02;
break;
case"3":
echo 03;
break;
case"4":
echo 04;
break;
case"5":
echo 05;
break;
case"6":
echo 06;
break;
case"7":
echo 07;
break;
case"8":
echo 08;
break;
case"9":
echo 09;
break;
case"10":
echo "0A";
break;
case"11":
echo "0B";
break;
case"12":
echo "0C";
break;
case"13":
echo "0D";
break;
case"14":
echo "0E";
break;
case"15":
echo "0F";
break;
}
}
else if($input > 15){
if($input >= 240){
$h1 = "F";
$x = $input -240;
}
else if($input >=224 and $input <=239){
$h1 = "E";
$x = $input -224;
}
else if($input >=208 and $input <=223){
$h1 = "D";
$x = $input -208;
}
else if($input >=192 and $input <=207){
$h1 = "C";
$x = $input -192;
}
else if($input >=176 and $input <=191){
$h1 = "B";
$x = $input -176;
}
else if($input >=160 and $input <=175){
$h1 = "A";
$x = $input -160;
}
else if($input >=144 and $input <=159){
$h1 = "9";
$x = $input -144;
}
else if($input >=128 and $input <=143){
$h1 = "8";
$x = $input -128;
}
else if($input >=112 and $input <=127){
$h1 = "7";
$x = $input -112;
}
else if($input >=96 and $input <=111){
$h1 = "6";
$x = $input -96;
}
else if($input >=80 and $input <=95){
$h1 = "5";
$x = $input -80;
}
else if($input >=64 and $input <=79){
$h1 = "4";
$x = $input -64;
}
else if($input >=48 and $input <=63){
$h1 = "3";
$x = $input -48;
}
else if($input >=32 and $input <=47){
$h1 = "2";
$x = $input -32;
}
else if($input >=16 and $input <=31){
$h1 = "1";
$x = $input -16;
}
if($x == 15){
$h0 = "F";
}
else if($x == 14){
$h0 = "E";
}
else if($x == 13){
$h0 = "D";
}
else if($x == 12){
$h0 = "C";
}
else if($x == 11){
$h0 = "B";
}
else if($x == 10){
$h0 = "A";
}
else if($x == 9){
$h0 = "9";
}
else if($x == 8){
$h0 = "8";
}
else if($x == 7){
$h0 = "7";
}
else if($x == 6){
$h0 = "6";
}
else if($x == 5){
$h0 = "5";
}
else if($x == 4){
$h0 = "4";
}
else if($x == 3){
$h0 = "3";
}
else if($x == 2){
$h0 = "2";
}
else if($x == 1){
$h0 = "1";
}
else if($x == 0 or $x == "" ){
$h0 = "0";
}
echo "แปลงเป็นเลขฐาน16 = $h1$h0";
}
?>