Base Conversion in PHP and JavaScript

Source: Internet
Author: User
Tags binary to decimal decimal to binary hex to binary hex to decimal html form php programming php programming language sprintf

http://www.exploringbinary.com/base-conversion-in-php-using-built-in-functions/

http://www.binaryconvert.com/convert_float.html?decimal=054046056050049051

Https://www.codeproject.com/Tips/387989/Convert-Binary-Single-Precision-Value-to-Float-in

function Floattoieee(F){ VarBuf= new arraybuffer4 (new float32arraybuf0]= F;return  (new uint32array (buf0< Span class= "pun" >; }             /span>                

Unfortunately, this doesn ' t work with doubles and the old browsers

function Doubletoieee(F){ VarBuf= New ArrayBuffer(8); (New Float64array(buf0] = F  return [  (new uint32array (buf< Span class= "pun")) [0] new uint32array (buf< Span class= "pun")) [1] ];               /span>                

-------------------------------------------------------------

The PHP programming language have many built-in functions for converting numbers from one base to another. In fact, it had so many functions the it can be the hard-to-know which to use. Some functions has similar capabilities, and Some work with parameters of different types. We ' ll sort through the differences in this article, and explain the proper context in which to use each function.

As a guide to our discussion, our mission would be-to-write programs, as input, an integer in a given base, and P Roduce, as output, the equivalent of that integer in another base. Both the input and output would be strings -sequences of characters with encodings like ASCII or EBCDIC. That's the form numbers take when read on from and written out to a user. Contrast this with numbers, can is operated on arithmetically inside a computer, numbers represented with binary Integ ers or floating-point binary. I call numbers in that form numeric binary.

Why the distinction between string and numeric binary? Because Both types is used in PHP ' s conversion functions. Knowing which parameters is which type is key to using the functions properly.

Summary of PHP Conversion Functions

PHP have built-in functions that can convert, or help convert, integers between string representations in various number BA Ses. We'll use them to convert between decimal (base), binary (base 2), hexadecimal (base), and octal (base 8). These is number bases that anyone versed in binary should know.

There ' s an important point I need to make before continuing. In PHP, and just about anywhere else, functions described as converting to or from decimal really convert to or from n Umeric binary! If you take the only one thing from this article, let's it.

Here are a summary of the PHP conversion functions used in this article, with a description of how they ' re used, and the MA Ximum integer They can safely convert:

PHP Functions for Base Conversion
Function Type of Conversion Max Integer
Bindec () Binary string to numeric binary 253
Hexdec () Hex string to numeric binary 253
Octdec () Octal string to numeric binary 253
Intval () Base 2 to $ string to numeric binary 231–1
SSCANF () Decimal, Hex, or octal string to numeric binary 231–1
Decbin () Numeric binary to binary string 232–1
Dechex () Numeric binary to hex string 232–1
DECOCT () Numeric binary to octal string 232–1
Strval () Numeric binary to decimal string Between 239 and 240
sprintf () Numeric binary to decimal, binary, hex, or octal string 253
Base_convert () Base 2 to $ string to base 2 to + string 253?

(? In the Base_convert () documentation, there are this warning: "base_convert () could lose precision on large numbers due to Properties related to the internal ' double ' or ' float ' type used. " If that leaves you wanting, it did me too.)

Conversion Code Examples

In the following sections, I give examples of conversions between specific base pairs. Look for the specific conversion in which you ' re interested.

The code, I use separate variables for the input string, the intermediate numeric binary value, and the output string. The separate string variables represent the I/O of a program, keeping the code independent of any particular I/O mechanism (HTML Form I/O, echo, printf, etc.). The separate variables also make clearer which parameters is which type.

The examples which use intval and sscanf limit, the maximum integer, can be converted-to 231–1. The the case, for example, and the code that converts from decimal to binary, even though decbin supports integers up t o 232–1. A Similar thing happens when composing the ' *dec ' and ' dec* ' functions. For example, Hexdec followed by Decbin are limited to 232–1 by Decbin.

The code does not a label input and output strings with their base (for example, with prefixes like 0b, 0x, or 0o). The base is implied with context.

Converting between Decimal and Binarydecimal to Binary

Here is three ways to convert a decimal string to a binary string using built-in functions:

    • Use Intval to convert the decimal string to numeric binary, and then use Decbinto convert the numeric binary value to a bi Nary string:
      <?php $decString = "42"; $binNumeric = Intval ($decString); $binString = Decbin ($binNumeric); = "101010"?>
    • Use SSCANF to convert the decimal string to numeric binary, and then use sprintf to convert the numeric binary value to a Binary string:
      <?php $decString = "32"; SSCANF ($decString, "%d",& $binNumeric); $binString = sprintf ("%b", $binNumeric); = "100000"?>

      Note:support of the%b format specifier is nonstandard.

    • Use Base_convert to convert the decimal string directly to a binary string:
      <?php $decString = "26"; $binString = Base_convert ($decString, 10,2); = "11010"?>
Binary to Decimal

Here is three ways to convert a binary string to a decimal string using built-in functions:

    • Use Bindec to convert the binary string to numeric binary, and then use SPRINTFTO convert the numeric binary value to a de Cimal string:
      <?php $binString = "11011110"; $binNumeric = Bindec ($binString); $decString = sprintf ("%.0f", $binNumeric); = "222"?>
    • Use Intval to convert the binary string to numeric binary, and then use Strvalto convert the numeric binary value to a Dec iMAL string:
      <?php $binString = "10100"; $binNumeric = Intval ($binString, 2); $decString = Strval ($binNumeric); = "?>"
    • Use Base_convert to convert the binary string directly to a decimal string:
      <?php $binString = "111000111001"; $decString = Base_convert ($binString, 2,10); = "3641"?>
Converting between Decimal and Hexadecimaldecimal to Hex

Here is three ways to convert a decimal string to a hexadecimal string using built-in functions:

    • Use Intval to convert the decimal string to numeric binary, and then use Dechex to convert the numeric binary value to a h Exadecimal string:
      <?php $decString = "42"; $binNumeric = Intval ($decString); $hexString = Dechex ($binNumeric); = "2a"?>
    • Use SSCANF to convert the decimal string to numeric binary, and then use sprintf to convert the numeric binary value to a Hexadecimal string:
      <?php $decString = "112"; SSCANF ($decString, "%d",& $binNumeric); $hexString = sprintf ("%x", $binNumeric); = "?>"
    • Use Base_convert to convert the decimal string directly to a hexadecimal string:
      <?php $decString = "25"; $hexString = Base_convert ($decString, 10,16); = "?>"
Hex to Decimal

Here is four ways to convert a hexadecimal string to a decimal string using built-in functions:

  • Use Hexdec to convert the hexadecimal string to numeric binary, and then use sprintf to convert the numeric binary value T o A decimal string:
    <?php $hexString = "de"; $binNumeric = Hexdec ($hexString); $decString = sprintf ("%.0f", $binNumeric); = "222"?>
  • Use Intval to convert the hexadecimal string to numeric binary, and then use Strval to convert the numeric binary value to A decimal string:
    <?php $hexString = "14"; $binNumeric = Intval ($hexString, 16); $decString = Strval ($binNumeric); = "?>"
  • Use SSCANF to convert the hexadecimal string to numeric binary, and then use Strval to convert the numeric binary value to A decimal string:
    <?php $hexString = "27"; SSCANF ($hexString, "%x",& $binNumeric); $decString = Strval ($binNumeric); = "?>"
  • Use Base_convert to convert the hexadecimal string directly to a decimal string:
    <?php $hexString = "25"; $decString = Base_convert ($hexString, 16,10); = "Panax Notoginseng"?>
Converting between Decimal and Octaldecimal to octal

Here is three ways to convert a decimal string to an octal string using built-in functions:

    • Use Intval to convert the decimal string to numeric binary, and then use Decoctto convert the numeric binary value to an O Ctal string:
      <?php $decString = "42"; $binNumeric = Intval ($decString); $octString = Decoct ($binNumeric); = "?>"
    • Use SSCANF to convert the decimal string to numeric binary, and then use sprintf to convert the numeric binary value Octal string:
      <?php $decString = "9"; SSCANF ($decString, "%d",& $binNumeric); $octString = sprintf ("%o", $binNumeric); = "One"?>
    • Use Base_convert to convert the decimal string directly to an octal string:
      <?php $decString = "25"; $octString = Base_convert ($decString, 10,8); = "?>"
Octal to Decimal

Here is four ways to convert an octal string to a decimal string using built-in functions:

  • Use Octdec to convert the octal string to numeric binary, and then use SPRINTFTO convert the numeric binary value to a Dec iMAL string:
    <?php $octString = "77"; $binNumeric = Octdec ($octString); $decString = sprintf ("%.0f", $binNumeric); = "?>"
  • Use Intval to convert the octal string to numeric binary, and then use Strval to convert the numeric binary value to a Dec iMAL string:
    <?php $octString = "14"; $binNumeric = Intval ($octString, 8); $decString = Strval ($binNumeric); = "?>"
  • Use SSCANF to convert the octal string to numeric binary, and then use Strval to convert the numeric binary value to a Dec iMAL string:
    <?php $octString = "14"; SSCANF ($octString, "%o",& $binNumeric); $decString = Strval ($binNumeric); = "?>"
  • Use Base_convert to convert the octal string directly to a decimal string:
    <?php $octString = "61"; $decString = Base_convert ($octString, 8,10); = "?>"
Converting between Power of Bases

You can use the functions above to convert between bases 2, 8, and from without going through decimal strings. One approach is to use Base_convert; Another is to compose the ' *dec ' and ' dec* ' functions.

Hex to Binary

Here is the ways to convert a hexadecimal string to a binary string using built-in functions:

    • Use Hexdec to convert the hexadecimal string to numeric binary, and then use Decbin to convert the numeric binary value to A binary string:
      <?php $hexString = "1f"; $binNumeric = Hexdec ($hexString); $binString = Decbin ($binNumeric); = "11111"?>
    • Use Base_convert to convert the hexadecimal string directly to a binary string:
      <?php $hexString = "ff"; $binString = Base_convert ($hexString, 16,2); = "11111111"?>
Binary to Hex

Here is the ways to convert a binary string to a hexadecimal string using built-in functions:

    • Use Bindec to convert the binary string to numeric binary, and then use Dechexto convert the numeric binary value to a hex Adecimal string:
      <?php $binString = "10011"; $binNumeric = Bindec ($binString); $hexString = Dechex ($binNumeric); = "?>"
    • Use Base_convert to convert the binary string directly to a hexadecimal string:
      <?php $binString = "1111"; $hexString = Base_convert ($binString, 2,16); = "F"?>
Octal to Binary

Here is the ways to convert an octal string to a binary string using built-in functions:

    • Use Octdec to convert the octal string to numeric binary, and then use Decbin to convert the numeric binary value to a bin ary string:
      <?php $octString = "77"; $binNumeric = Octdec ($octString); $binString = Decbin ($binNumeric); = "111111"?>
    • Use Base_convert to convert the octal string directly to a binary string:
      <?php $octString = "71"; $binString = Base_convert ($octString, 8,2); = "111001"?>
Binary to octal

Here is the ways to convert a binary string to an octal string using built-in functions:

    • Use Bindec to convert the binary string to numeric binary, and then use Decoctto convert the numeric binary value to an OC Tal string:
      <?php $binString = "1010"; $binNumeric = Bindec ($binString); $octString = Decoct ($binNumeric); = "?>"
    • Use Base_convert to convert the binary string directly to an octal string:
      <?php $binString = "11011"; $octString = Base_convert ($binString, 2,8); = "?>"
Octal to Hex

Here is the ways to convert an octal string to a hexadecimal string using built-in functions:

    • Use Octdec to convert the octal string to numeric binary, and then use Dechexto convert the numeric binary value to a hexa Decimal string:
      <?php $octString = "77"; $binNumeric = Octdec ($octString); $hexString = Dechex ($binNumeric); = "3f"?>
    • Use Base_convert to convert the octal string directly to a hexadecimal string:
      <?php $octString = "123"; $hexString = Base_convert ($octString, 8,16); = "?>"
Hex to octal

Here is the ways to convert a hexadecimal string to an octal string using built-in functions:

    • Use Hexdec to convert the hexadecimal string to numeric binary, and then use DECOCT to convert the numeric binary value to An octal string:
      <?php $hexString = "7d8"; $binNumeric = Hexdec ($hexString); $octString = Decoct ($binNumeric); = "3730"?>
    • Use Base_convert to convert the hexadecimal string directly to an octal string:
      <?php $hexString = "F0"; $octString = Base_convert ($hexString, 16,8); = "?>"

Base Conversion in PHP and JavaScript

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.