Example of base_convert ()-based numeric conversion function in php

Source: Internet
Author: User
This article mainly introduces the base_convert () numeric conversion function in php, and demonstrates the conversion method between the decimal number and the binary number, the octal value and the hexadecimal value in the form of an example, it has good reference value. if you need it, you can refer to the following example to describe how to implement the base_convert () function in php. Share it with you for your reference. The details are as follows:

Syntax: base_convert (number, frombase, tobase)

Parameters Description
Number Required, original value.
Frombase Required. number in the original hexadecimal format.
Tobase Required. the hexadecimal format to be converted.

The PHP instance code is as follows:

The code is as follows:

$ Hexadecimal = 'a37334 ';
Echo base_convert ($ hexadecimal, 16,2); // Convert to binary output 101000110111001100110100
Echo"
";
$ Number = "123 ";
Echo base_convert ($ number, 1111011); // Convert to binary output
Echo"
";
Echo base_convert ($ number, 10, 8); // Convert to octal output 173
Echo"
";
Echo base_convert ($ number, 10, 16); // Convert to hexadecimal output 7b
$ Number2 = "100000101 ";
Echo"
";
Echo base_convert ($ number2, 2, 10); // Convert to decimal output 261
Echo"
";
Echo base_convert ($ number2, 2, 8); // Convert to octal output 405


Description: return a string that contains the tobase representation of the number. The number system is specified by frombase. both frombase and tobase can only be between 2 and 36 (including 2 and 36). numbers higher than decimal digits are represented by a-z, for example, a indicates 10, B indicates 11, and z indicates 35.

I hope this article will help you with PHP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.