热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:BaseConversionInPHPandjavascript

篇首语:本文由编程笔记#小编为大家整理,主要介绍了Base Conversion In PHP and Javascript相关的知识,希望对你有一定的参考价值。 http://www.explori

篇首语:本文由编程笔记#小编为大家整理,主要介绍了Base Conversion In PHP and Javascript相关的知识,希望对你有一定的参考价值。


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
FunctionType of ConversionMax Integer
bindec()Binary string to numeric binary253
hexdec()Hex string to numeric binary253
octdec()Octal string to numeric binary253
intval()Base 2 to 36 string to numeric binary231 – 1
sscanf()Decimal, hex, or octal string to numeric binary231 – 1
decbin()Numeric binary to binary string232 – 1
dechex()Numeric binary to hex string232 – 1
decoct()Numeric binary to octal string232 – 1
strval()Numeric binary to decimal stringBetween 239 and 240
sprintf()Numeric binary to decimal, binary, hex, or octal string253
base_convert()Base 2 to 36 string to base 2 to 36 string253 ?

(? 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:

    $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:

    $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:

    $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:

    $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:

    $binString = "10100";
    $binNumeric = intval($binString,2);
    $decString = strval($binNumeric); // = "20"
    ?>


  • Use base_convert to convert the binary string directly to a decimal string:

    $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:

    $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:

    $decString = "112";
    sscanf($decString,"%d",&$binNumeric);
    $hexString = sprintf ("%x",$binNumeric); // = "70"
    ?>


  • Use base_convert to convert the decimal string directly to a hexadecimal string:

    $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:

    $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:

    $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:

    $hexString = "27";
    sscanf($hexString,"%x",&$binNumeric);
    $decString = strval($binNumeric); // = "39"
    ?>


  • Use base_convert to convert the hexadecimal string directly to a decimal string:

    $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:

    $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:

    $decString = "9";
    sscanf($decString,"%d",&$binNumeric);
    $octString = sprintf ("%o",$binNumeric); // = "11"
    ?>


  • Use base_convert to convert the decimal string directly to an octal string:

    $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:

    $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:

    $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:

    $octString = "14";
    sscanf($octString ,"%o",&$binNumeric);
    $decString = strval($binNumeric); // = "12"
    ?>


  • Use base_convert to convert the octal string directly to a decimal string:

    $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:

    $hexString = "1f";
    $binNumeric = hexdec($hexString);
    $binString = decbin($binNumeric); // = "11111"
    ?>


  • Use base_convert to convert the hexadecimal string directly to a binary string:

    $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:

    $binString = "10011";
    $binNumeric = bindec($binString);
    $hexString = dechex($binNumeric); // = "13"
    ?>


  • Use base_convert to convert the binary string directly to a hexadecimal string:

    $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:

    $octString = "77";
    $binNumeric = octdec($octString);
    $binString = decbin($binNumeric); // = "111111"
    ?>


  • Use base_convert to convert the octal string directly to a binary string:

    $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:

    $binString = "1010";
    $binNumeric = bindec($binString);
    $octString = decoct($binNumeric); // = "12"
    ?>


  • Use base_convert to convert the binary string directly to an octal string:

    $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:

    $octString = "77";
    $binNumeric = octdec($octString);
    $hexString = dechex($binNumeric); // = "3f"
    ?>


  • Use base_convert to convert the octal string directly to a hexadecimal string:

    $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:

    $hexString = "7d8";
    $binNumeric = hexdec($hexString);
    $octString = decoct($binNumeric); // = "3730"
    ?>


  • Use base_convert to convert the hexadecimal string directly to an octal string:

    $hexString = "f0";
    $octString = base_convert($hexString,16,8); // = "360"
    ?>



推荐阅读
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文详细介绍了GetModuleFileName函数的用法,该函数可以用于获取当前模块所在的路径,方便进行文件操作和读取配置信息。文章通过示例代码和详细的解释,帮助读者理解和使用该函数。同时,还提供了相关的API函数声明和说明。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 从零学Java(10)之方法详解,喷打野你真的没我6!
    本文介绍了从零学Java系列中的第10篇文章,详解了Java中的方法。同时讨论了打野过程中喷打野的影响,以及金色打野刀对经济的增加和线上队友经济的影响。指出喷打野会导致线上经济的消减和影响队伍的团结。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
author-avatar
DSSDD
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有