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)
{
var buf = new ArrayBuffer(4);
(new Float32Array(buf))[0] = f;
return (new Uint32Array(buf))[0];
}

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

function DoubleToIEEE(f)
{
var buf = new ArrayBuffer(8);
(new Float64Array(buf))[0] = f;
return [ (new Uint32Array(buf))[0] ,(new Uint32Array(buf))[1] ];
}

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

The PHP programming language has many built-in functions for converting numbers from one base to another. In fact, it has so many functions that it can be hard to know which to use. Some functions have 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 will be to write programs that take, as input, an integer in a given base, and produce, as output, the equivalent of that integer in another base. Both the input and output will be strings — sequences of characters with encodings like ASCII or EBCDIC. That is the form numbers take when read in from and written out to a user. Contrast this with numbers that can be operated on arithmetically inside a computer, numbers represented with binary integers or floating-point binary. I call numbers in that form numeric binary.

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

Summary of PHP Conversion Functions

PHP has built-in functions that can convert, or help convert, integers between string representations in various number bases. We will use them to convert between decimal (base 10), binary (base 2), hexadecimal (base 16), and octal (base 8). These are 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 numeric binary! If you take only one thing from this article, let that be it.

Here is a summary of the PHP conversion functions used in this article, with a description of how they’re used, and the maximum 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 36 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 36 string to base 2 to 36 string 253 †

(† In the base_convert() documentation, there is this warning: “base_convert() may 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.

In 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 are which type.

The examples which use intval and sscanf limit the maximum integer that can be converted — to 231 – 1. This is the case, for example, in the code that converts from decimal to binary, even though decbin supports integers up to 232 – 1. A similar thing happens when composing the `*dec’ and `dec*’ functions. For example, hexdec followed by decbin is limited to 232 – 1 by decbin.

The code does not 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 Binary

Decimal to Binary

Here are 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 binary 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 are 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 decimal 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 decimal string:
    <?php
    $binString = "10100";
    $binNumeric = intval($binString,2);
    $decString = strval($binNumeric); // = "20"
    ?>
  • 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 Hexadecimal

Decimal to Hex

Here are 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 hexadecimal 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); // = "70"
    ?>
  • Use base_convert to convert the decimal string directly to a hexadecimal string:
    <?php
    $decString = "25";
    $hexString = base_convert($decString,10,16); // = "19"
    ?>

Hex to Decimal

Here are 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 to 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); // = "20"
    ?>
  • 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); // = "39"
    ?>
  • Use base_convert to convert the hexadecimal string directly to a decimal string:
    <?php
    $hexString = "25";
    $decString = base_convert($hexString,16,10); // = "37"
    ?>

Converting Between Decimal and Octal

Decimal to Octal

Here are 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 octal string:

    <?php
    $decString = "42";
    $binNumeric = intval($decString);
    $octString = decoct($binNumeric); // = "52"
    ?>
  • Use sscanf to convert the decimal string to numeric binary, and then use sprintf to convert the numeric binary value to an octal string:
    <?php
    $decString = "9";
    sscanf($decString,"%d",&$binNumeric);
    $octString = sprintf ("%o",$binNumeric); // = "11"
    ?>
  • Use base_convert to convert the decimal string directly to an octal string:
    <?php
    $decString = "25";
    $octString = base_convert($decString,10,8); // = "31"
    ?>

Octal to Decimal

Here are 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 decimal string:

    <?php
    $octString = "77";
    $binNumeric = octdec($octString);
    $decString = sprintf("%.0f",$binNumeric); // = "63"
    ?>
  • Use intval to convert the octal string to numeric binary, and then use strval to convert the numeric binary value to a decimal string:
    <?php
    $octString = "14";
    $binNumeric = intval($octString ,8);
    $decString = strval($binNumeric); // = "12"
    ?>
  • Use sscanf to convert the octal string to numeric binary, and then use strval to convert the numeric binary value to a decimal string:
    <?php
    $octString = "14";
    sscanf($octString ,"%o",&$binNumeric);
    $decString = strval($binNumeric); // = "12"
    ?>
  • Use base_convert to convert the octal string directly to a decimal string:
    <?php
    $octString = "61";
    $decString = base_convert($octString,8,10); // = "49"
    ?>

Converting Between Power of Two Bases

You can use the functions above to convert between bases 2, 8, and 16 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 are two 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 are two 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 hexadecimal string:

    <?php
    $binString = "10011";
    $binNumeric = bindec($binString);
    $hexString = dechex($binNumeric); // = "13"
    ?>
  • 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 are two 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 binary 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 are two 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 octal string:

    <?php
    $binString = "1010";
    $binNumeric = bindec($binString);
    $octString = decoct($binNumeric); // = "12"
    ?>
  • Use base_convert to convert the binary string directly to an octal string:
    <?php
    $binString = "11011";
    $octString = base_convert($binString,2,8); // = "33"
    ?>

Octal to Hex

Here are two 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 hexadecimal 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); // = "53"
    ?>

Hex to Octal

Here are two 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); // = "360"
    ?>

Base Conversion In PHP and javascript的更多相关文章

  1. NUMBER BASE CONVERSION(进制转换)

    Description Write a program to convert numbers in one base to numbers in a second base. There are 62 ...

  2. poj 1220 NUMBER BASE CONVERSION(短除法进制转换)

    题目连接:1220 NUMBER BASE CONVERSION 题目大意:给出两个进制oldBase 和newBase, 以及以oldBase进制存在的数.要求将这个oldBase进制的数转换成ne ...

  3. poj 1220 NUMBER BASE CONVERSION

    NUMBER BASE CONVERSION Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5976   Accepted: ...

  4. POJ1220 Number Base Conversion

    题意 Write a program to convert numbers in one base to numbers in a second base. There are 62 differen ...

  5. (高精度运算4.7.26)POJ 1220 NUMBER BASE CONVERSION(高精度数的任意进制的转换——方法:ba1----->10进制----->ba2)

    package com.njupt.acm; import java.math.BigInteger; import java.util.Scanner; public class POJ_1220_ ...

  6. POJ 1220 NUMBER BASE CONVERSION(较复杂的进制转换)

    题目链接 题意 : 给你一个a进制的数串s,让你转化成b进制的输出. A = 10, B = 11, ..., Z = 35, a = 36, b = 37, ..., z = 61,0到9还是原来的 ...

  7. SZU:J38 Number Base Conversion

    Judge Info Memory Limit: 32768KB Case Time Limit: 1000MS Time Limit: 1000MS Judger: Number Only Judg ...

  8. UVa 10473 - Simple Base Conversion

    题目大意:十进制与十六进制之间的相互转换. #include <cstdio> int main() { #ifdef LOCAL freopen("in", &quo ...

  9. [POJ1220]NUMBER BASE CONVERSION (高精,进制转换)

    题意 任意进制之间的高进的转换 思路 相模倒排,高精处理 代码 我太弱了,下面附一个讨论里发的maigo思路的代码 ],A[]; ],d[]; main(){ for(scanf("%d&q ...

随机推荐

  1. 用Python控制摄像头拍照并发邮件

    概述前言 工具 思路 安装及导入包 设置参数 实现拍照 构造邮件内容 发送邮件 判断网络连接 开机自启 后记 o1 前言为什么会有写这个程序的想法呢? 最初的想法是写一个可以用电脑前置摄像头拍照的程序 ...

  2. CSS3 四边形 凹角写法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. docker 容器挂载主机目录,访问出现 cannot open directory /mnt/home/webroot/: Permission denied 的解决办法

    问题原因及解决办法 原因是CentOS7中的安全模块selinux把权限禁掉了,至少有以下三种方式解决挂载的目录没有权限的问题: 1.在运行容器的时候,给容器加特权,及加上 --privileged= ...

  4. Windows:32位程序运行在64位系统上注册表会重定向

    参考资料 微软注册表英文文档 StackOverflow社区回答 1.注册表位置 64bit系统(Windows Server 2008 R2只有64bit系统)的注册表分32 位注册表项和64位注册 ...

  5. vue脚手架引入swiper

    方法一: 下载swiper: npm install swiper --save-dev swiper4.0使用入口:http://www.swiper.com.cn/usage/index.html ...

  6. 从Excel读取数据,然后分析相似的数据,多线程处理(多线程比较相似的字符串,统计出相似的数量及字符串)

    之前的jar包有问题,现已修改. 需要的jar包,已修改 自己去Maven中央仓库下载jar包. excel数据: 直接上代码. 程序再度优化了一遍.之后如果想再度精准,可能需要建模,最近没空继续做了 ...

  7. 流行-Manifold【0】-维基百科中文版本解释

  8. install docker on centos7

    copy from:https://www.youtube.com/watch?v=pm55BUwQ0iE # Prerequisites - Kernel must be 3.10 at minim ...

  9. 笔试算法题(04):实现 string & memcpy & strcpy & strlen

    出题:请实现给定String的类定义: 分析:注意检查标准类构造注意事项: 解题: #include <stdio.h> #include <string.h> /** * 检 ...

  10. <Redis> 入门三 事务

    Redis事务是什么 1.可以一次执行多个命令,本质是一组命令的集合. 2.一个事务中的所有命令都会被序列化,按顺序串行化执行而不会被其他命令插入,不许加塞. 意味着redis在事务执行的过程中,不允 ...