php将任意进制数转换成10进制的方法

墨辰丷
发布: 2018-06-11 15:24:07
原创
2037人浏览过

这篇文章主要介绍了php实现将任意进制数转换成10进制的方法,涉及php数制转换的相关技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了php实现将任意进制数转换成10进制的方法。分享给大家供大家参考。具体如下:

php将任意进制的数转换成10进制,例如8进制转换成10进制,16进制转换成10进制

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

<?php

# Show the steps involved in converting a number

# from any base (like octal or hex) to base 10

# See below for examples, instructions and copyright

function show_convert_to_base_10 ($number, $base)

{

 // If the number contains a decimal component

 if (strstr ($number, '.'))

 {

  // Get the integer and decimal components

  list ($integer, $decimal) = explode ('.', $number);

 }

 else

 {

  // The number is an integer

  $integer = $number;

 }

  print "<b>Convert the base $base number $number to a

  base 10 number:</b><blockquote>";

  print "Convert the integer component ($integer) of the

   number:";

 // Compute the value of the integer component

 // Loop through the integer digit by digit

 // Reverse the number for easier handling

 $integer = strrev ($integer);

 $length = strlen ($integer);

 for ($pos = 0; $pos </blockquote>';

 if (isset ($decimal))

 {

   print "Convert the decimal component (0.$decimal)

   of the number:<blockquote>";

  // Pad the number with a leading 0 so that we can

  // start at position 1

  $decimal = '0'.$decimal;

  $length = strlen ($decimal);

   for ($pos = 1; $pos < $length; ++$pos) {

   $digit = $decimal[$pos];

   // Handle character values for digits

   // (for bases greater than 10)

   if (eregi ('[a-z]', $digit))

   {

     $digit_value =

     (ord (strtolower ($digit))

     - ord ('a')) + 10;

      $digit = "$digit ($digit_value)";

   }

   else

   {

     $digit_value = $digit;

   }

   // Multiply the current digit by the radix

   // raised to the power of the current position

   $result = $digit_value * pow (1/$base, $pos);

    print "Multiply the value of the digit at

    position $pos by the value of the 1/radix

    ($base) raised to the power of the position

    ($pos):<br/>";

    print "$digit * 1/$base<sup>$pos</sup> =

    $result<br/><br/>";

    $sums[] = $result;

  }

  print '</blockquote>';

 }

 $sums = implode (' + ', $sums);

 eval ("\$base_10_value = $sums;");

  print "</blockquote>The value of the base $base number

  $number in base 10 is $base_10_value. <br/>";

  print "This number is derived from the sum of the values

  of the previous operations ($sums). <br/> <br/>";

}

登录后复制

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

立即学习PHP免费学习笔记(深入)”;

php针对数据库的读取、判断及session登陆的使用方法

php实现网页缓存的工具类的代码及使用方法

php文件上传所涉及的文件与表单操作及数据库操作

以上就是php将任意进制数转换成10进制的方法的详细内容,更多请关注php中文网其它相关文章!

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号