PHP实现在线进制转换器,10进制,2、4、8、16、32进制转换

1.接口文档 

 

 2.laravel实现代码

 /*** 进制转换计算器* @return \Illuminate\Http\JsonResponse*/public function binaryConvertCal(){$ten = $this->request('ten');$two = $this->request('two');$four = $this->request('four');$eight = $this->request('eight');$sixteen = $this->request('sixteen');$thirty_two = $this->request('thirty_two');$type = intval($this->request('type')) ?: 10;$data = ['ten' => $ten,'two' => $two,'four' => $four,'eight' => $eight,'sixteen' => $sixteen,'thirty_two' => $thirty_two];if ($type == 10 && $ten) {// 1.十进制$data['ten'] = $ten;// 2.将十进制转换二进制$data['two'] = decbin($ten);// 3.十进制转换四进制$data['four'] = $this->decimalToBase($ten, 4);// 4.十进制转换八进制$data['eight'] = decoct($ten);// 5.十进制转换十六进制$data['sixteen'] = dechex($ten);;// 6.十进制转换三十二进制$data['thirty_two'] = $this->base10ToBase32($ten);}if ($type == 2 && $two) {// 1.二进制转换十进制$ten = bindec($two);$data['ten'] = $ten ?: 'NAN';// 2.二进制$data['two'] = $two;// 3.二进制转换为四进制$fourNumber = bindec($two); // 转换为10进制$fourNumber = base_convert($fourNumber, 10, 4); // 转换为4进制$data['four'] = $fourNumber ?: 'NAN';// 4.二进制转换为八进制$eightNumber = bindec($two); // 转换为10进制$eight = base_convert($eightNumber, 10, 8); // 转换为8进制$data['eight'] = $eight ?: 'NAN';// 5.二进制转换为十六进制$sixteenNumber = bindec($two); // 转换为10进制$sixteen = base_convert($sixteenNumber, 10, 16); // 转换为16进制$data['sixteen'] = $sixteen ?: 'NAN';// 6.二进制转换为三十二进制$decimalNumber = bindec($two); // 将二进制数字转换为十进制$thirty_two = base_convert($decimalNumber, 10, 32); // 将十进制数字转换为32进制$data['thirty_two'] = $thirty_two ?: 'NAN';}if ($type == 4 && $four) {// 1.四进制转换十进制$ten = $this->base4_to_decimal($four);$data['ten'] = $ten ?: 'NAN';// 2.四进制转换为二进制$two = $this->base4_to_binary($four);$data['two'] = $two ?: 'NAN';// 3.四进制$data['four'] = $four;// 4.四进制转换为八进制$eight = $this->base4_to_octal($four);$data['eight'] = $eight ?: 'NAN';// 5.四进制转换为十六进制$sixteen = $this->base4_to_hexadecimal($four);$data['sixteen'] = $sixteen ?: 'NAN';// 6.四进制转换为三十二进制$thirty_two = $this->base4_to_base32($four);$data['thirty_two'] = $thirty_two ?: 'NAN';}if ($type == 8 && $eight) {// 1. 8进制转换为10进制$ten = octdec($eight);$data['ten'] = $ten ?: 'NAN';// 2. 8制转换为二进制$two = base_convert($eight, 8, 2);$data['two'] = $two ?: 'NAN';// 3. 8进制转换为4进制$four = base_convert($eight, 8, 4);$data['four'] = $four ?: 'NAN';// 4. 8进制$data['eight'] = $eight;// 5. 8进制转换为16进制$sixteen = base_convert($eight, 8, 16);$data['sixteen'] = $sixteen ?: 'NAN';// 6. 8进制转换为32进制$base32Chars = '0123456789ABCDEFGHIJKLMNOPQRSTUV';$base32Number = '';$decimalNumber = octdec($eight);while ($decimalNumber > 0) {$remainder = $decimalNumber % 32;$base32Number = $base32Chars[$remainder] . $base32Number;$decimalNumber = intdiv($decimalNumber, 32);}$data['thirty_two'] = $base32Number ?: 'NAN';}if ($type == 16 && $sixteen) {// 1. 将16进制转换为10进制$ten = hexdec($sixteen);$data['ten'] = $ten ?: 'NAN';// 2. 16制转换为2进制$two = base_convert($sixteen, 16, 2);$data['two'] = $two ?: 'NAN';// 3. 16进制转换为4进制$four = base_convert($sixteen, 16, 4);$data['four'] = $four ?: 'NAN';// 4. 16进制转换为8进制$eight = base_convert($sixteen, 16, 8);$data['eight'] = $eight;// 5. 16进制$data['sixteen'] = $sixteen;// 6. 将16进制转换为32进制$thirty_two = $this->hexToBase32($sixteen);$data['thirty_two'] = $thirty_two ?: 'NAN';}if ($type == 32 && $thirty_two) {// 1. 将32进制转换为10进制$ten = $this->base32ToDecimal($thirty_two);$data['ten'] = $ten ?: 'NAN';// 2. 32制转换为2进制$two = $this->base32ToBinary($thirty_two);$data['two'] = $two ?: 'NAN';// 3. 32进制转换为4进制$four = $this->base32ToQuaternary($thirty_two);$data['four'] = $four ?: 'NAN';// 4. 32进制转换为8进制$eight = $this->base32ToOctal($thirty_two);$data['eight'] = $eight;// 5. 32进制转换为16进制$data['sixteen'] = $this->base32ToHex($thirty_two);// 6. 32进制$data['thirty_two'] = $thirty_two;}return $this->jsonSuc(['result' => $data]);}/*** 十进制转换* @param $decimal* @param $base* @return string*/function decimalToBase($decimal, $base){$result = '';while ($decimal > 0) {$remainder = $decimal % $base;$result = $remainder . $result;$decimal = (int)($decimal / $base);}return $result;}/*** 十进制转换为三十二* @param $num* @return string*/function base10ToBase32($num){$chars = '0123456789ABCDEFGHIJKLMNOPQRSTUV';$base32 = '';while ($num > 0) {$remainder = $num % 32;$base32 = $chars[$remainder] . $base32;$num = (int)($num / 32);}return ($base32 === '') ? '0' : $base32;}/*** 4转换为10进制* @param $num* @return float|int*/function base4_to_decimal($num){$decimal = 0;$base = 4;$num_length = strlen($num);for ($i = 0; $i < $num_length; $i++) {$digit = intval($num[$i]);$decimal += $digit * pow($base, $num_length - $i - 1);}return $decimal;}/*** 4转换为2进制* @param $num* @return string*/function base4_to_binary($num){$decimal = $this->base4_to_decimal($num);return decbin($decimal);}/*** 4转换为8进制* @param $num* @return string*/function base4_to_octal($num){$decimal = $this->base4_to_decimal($num);return decoct($decimal);}/*** 4转换为16进制* @param $num* @return string*/function base4_to_hexadecimal($num){$decimal = $this->base4_to_decimal($num);return dechex($decimal);}/*** 4转换为32进制* @param $num* @return string*/function base4_to_base32($num){$decimal = $this->base4_to_decimal($num);return base_convert($decimal, 10, 32);}/*** 16转32进制* @param $hex* @return string*/function hexToBase32($hex){$base32Chars = '0123456789ABCDEFGHIJKLMNOPQRSTUV';$result = '';// 将十六进制字符串拆分成两个字符一组$hexPairs = str_split($hex, 2);// 将每个十六进制对转换为十进制,并将其转换为五位长的三十二进制数foreach ($hexPairs as $pair) {$dec = hexdec($pair);$base32 = '';while ($dec >= 32) {$rem = $dec % 32;$base32 = $base32Chars[$rem] . $base32;$dec = (int)($dec / 32);}$base32 = $base32Chars[$dec] . $base32;$result .= $base32;}return $result;}/*** 将32进制转换为10进制* @param string $num 32进制字符串* @return int 转换后的10进制数值*/function base32ToDecimal($num){$base32_chars = "0123456789abcdefghijklmnopqrstuv";$dec = 0;$len = strlen($num);for ($i = 0; $i < $len; $i++) {$pos = strpos($base32_chars, $num[$i]);$dec = 32 * $dec + $pos;}return $dec;}/*** 将32进制转换为2进制* @param string $num 32进制字符串* @return string 转换后的2进制字符串*/function base32ToBinary($num){$dec = $this->base32ToDecimal($num);$binary = decbin($dec);return $binary;}/*** 将32进制转换为4进制* @param string $num 32进制字符串* @return string 转换后的4进制字符串*/function base32ToQuaternary($num){$dec = $this->base32ToDecimal($num);$quaternary = base_convert($dec, 10, 4);return $quaternary;}/*** 将32进制转换为8进制* @param string $num 32进制字符串* @return string 转换后的8进制字符串*/function base32ToOctal($num){$dec = $this->base32ToDecimal($num);$octal = decoct($dec);return $octal;}/*** 将32进制转换为16进制* @param string $num 32进制字符串* @return string 转换后的16进制字符串*/function base32ToHex($num){$dec = $this->base32ToDecimal($num);$hex = dechex($dec);return $hex;}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/61305.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Redis的AOF持久化

除了RDB持久化功能之外&#xff0c;Redis还提供了AOF持久化功能。与RDB 持久化通过保存数据库中的键值对来记录数据库状态不同&#xff0c;AOF持久化是通过保存Redis服务器所执行的写命令来记录数据库状态的&#xff0c;如下图所示。 举个例子&#xff0c;如果我们对空白的数据…

什么是DNS欺骗及如何进行DNS欺骗

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、什么是 DNS 欺骗&#xff1f;二、开始1.配置2.Ettercap启动3.操作 总结 前言 我已经离开了一段时间&#xff0c;我现在回来了&#xff0c;我终于在做一个教…

Centos8上加速git clone

首先通过命令获取域名对应的IP地址 [rootggbond ~]# nslookup github.global.ssl.fastly.net [rootggbond ~]# nslookup github.com 之后如上获取到的IP地址 以IP-域名的格式加入到hosts文件中 [rootggbond ~]# vim /etc/hosts Centos8上更新DNS缓存 [rootggbond ~]# nscd -…

Java课题笔记~ HTTP协议(请求和响应)

Servlet最主要的作用就是处理客户端请求&#xff0c;并向客户端做出响应。为此&#xff0c;针对Servlet的每次请求&#xff0c;Web服务器在调用service()方法之前&#xff0c;都会创建两个对象 分别是HttpServletRequest和HttpServletResponse。 其中HttpServletRequest用于封…

腾讯云香港服务器租用价格_CN2线路延迟速度测试

腾讯云香港服务器&#xff0c;目前中国香港地域轻量应用服务器可选配置2核2G20M、2核2G30M、2核4G30M&#xff0c;操作系统可选Windows和Linux&#xff0c;不只是香港云服务器&#xff0c;新加坡、硅谷、法兰克福和东京服务器均有活动&#xff0c;腾讯云服务器网分享腾讯云境外…

Node.js学习笔记-03

七、网络编程 1. 构建 TCP 服务 TCP 是面向连接的协议&#xff0c;显著特征 在传输之前需要3次握手形成会话。 客户端 ——请求连接——> 服务器端 ——响应——> 客户端 ——开始传输——> 服务器端。 2. 构建 UDP 服务 3. 构建 HTTP 服务 http模块 在node中HTT…

dotNet 之数据库sqlite

Sqlite3是个特别好的本地数据库&#xff0c;体积小&#xff0c;无需安装&#xff0c;是写小控制台程序最佳数据库。NET Core是同样也是.NET 未来的方向。 **硬件支持型号 点击 查看 硬件支持 详情** DTU701 产品详情 DTU702 产品详情 DTU801 产品详情 DTU802 产品详情 D…

pythonocc进阶学习:投影projection

1.点 到 线,&#xff08;直线,曲线&#xff09;等上的投影 staticmethod # 点到Lin的投影 def Project_Pnt_To_Lin(p: gp_Pnt, lin: gp_Lin):Edge BRepBuilderAPI_MakeEdge(lin).Edge()curve BRep_Tool.Curve(Edge)proPnt GeomAPI_ProjectPointOnCurve(p, curve[0])Neares…

基于Java+SpringBoot+Vue的数码论坛系统设计与实现(源码+LW+部署文档等)

博主介绍&#xff1a; 大家好&#xff0c;我是一名在Java圈混迹十余年的程序员&#xff0c;精通Java编程语言&#xff0c;同时也熟练掌握微信小程序、Python和Android等技术&#xff0c;能够为大家提供全方位的技术支持和交流。 我擅长在JavaWeb、SSH、SSM、SpringBoot等框架…

使用IIS服务器部署Flask python Web项目

参考文章 ""D:\Program Files (x86)\Python310\python310.exe"|"D:\Program Files (x86)\Python310\lib\site-packages\wfastcgi.py"" can now be used as a FastCGI script processor参考文章 请求路径填写*&#xff0c;模块选择FastCgiModule&…

【手撕C语言】多线程

(꒪ꇴ꒪ )&#xff0c;Hello我是祐言QAQ我的博客主页&#xff1a;C/C语言,Linux基础,ARM开发板&#xff0c;软件配置等领域博主&#x1f30d;快上&#x1f698;&#xff0c;一起学习&#xff0c;让我们成为一个强大的攻城狮&#xff01;送给自己和读者的一句鸡汤&#x1f914;&…

SpringMVC注解配置

1xml配置方式&#xff08;配置文件注解的方式&#xff09; 前提导入相关依赖&#xff1a;pom文件 说明&#xff1a;下方依赖是ssm项目中较为常用的一部分&#xff0c;可能部分依赖对于springmvc配置并未有关系&#xff0c;根据自己需求添加和删除。 <dependencies> &l…