百科狗-知识改变命运!
--

base_convert() - 在任意进制之间转换数字 - php 数学函数

梵高1年前 (2023-11-21)阅读数 49#技术干货
文章标签表示

base_convert()

(PHP 4, PHP 5, PHP 7)

在任意进制之间转换数字

说明

base_convert(string $number,int $frombase,int $tobase): string

返回一字符串,包含$number以$tobase进制的表示。$number本身的进制由$frombase指定。$frombase和$tobase都只能在 2 和 36 之间(包括 2 和 36)。高于十进制的数字用字母 a-z 表示,例如 a 表示 10,b 表示 11 以及 z 表示 35。

Warning

由于使用内部的"double"或"float"类型,base_convert()的操作可能会导致大数值中的精度丢失。请参见本手册的浮点数章节以便获得更多详细信息。

参数

$number

要转换的数字

$frombase

The base$numberis in

$tobase

The base to convert$numberto

返回值

base_convert() - 在任意进制之间转换数字 - php 数学函数

$numberconverted to base$tobase

范例

Example #1base_convert()例子

以上例程会输出:

101000110111001100110100

参见

  • intval() 获取变量的整数值
Convert an arbitrarily large number from any base to any base.
string convBase(string $numberInput, string $fromBaseInput, string $toBaseInput)
$numberInput number to convert as a string
$fromBaseInput base of the number to convert as a string
$toBaseInput base the number should be converted to as a string
examples for $fromBaseInput and $toBaseInput
'0123456789ABCDEF' for Hexadecimal (Base16)
'0123456789' for Decimal (Base10)
'01234567' for Octal (Base8)
'01' for Binary (Base2) 
You can really put in whatever you want and the first character is the 0.
Examples:

Typical use-cases:
1. Convert a large arbitrary precision number to base-36.
2. Convert a 32-char hexadecimal UUID (128-bit) to a 25-char base-36 unique key, and vice versa.
Examples:

ABCDEF00001234567890 (hexadecimal) = 3o47re02jzqisvio (base-36) 
ABCDEF01234567890123456789ABCDEF (hexadecimal) = a65xa07491kf5zyfpvbo76g33 (base-36)
If you need to use base_convert with numbers larger then 32 bit, the following gmp implementation of base_convert should work. 
If you would like to convert numbers into just the uppercase alphabet base and vice-versa (e.g. the column names in a Microsoft Windows Excel sheet..A-Z, AA-ZZ, AAA-ZZZ, ...), the following functions will do this.

Microsoft Windows Excel stops at IV (255), but this function can handle much larger. However, English words will start to form after a while and some may be offensive, so be careful.
a binary coded decimal is converted by taking groups of four from a decimal string,
for example the binary coded decimal string
1000 = 8
10001000 does not = 136 but 88
so
binarycodedstring2dec(1000) = 8
binarycodedstring2dec(11100000111001)=3839
binarycodedstring2dec(100000111001)=839
i truly have no idea if this function will be useful to anyone, i simply failed a physics midterm because i didn't know this so i wrote this function to make sure i would never forget how to convert binary coded decimals
Base62x gives an alternative way for traditional Base62 and/or Base64.
base62x_encode / base62x_decode would be merged into PHP as an extra module in the future.
Prior to that, users can download the PHP extra module via: https://github.com/wadelau/Base62x
A RFC 4648 Compliant Base32 encode. Send me an email if you end up making this thing more efficient with bitwise operations and whatever else. 
Really huge numbers might be truncated at both ends.
eg:

will output:
9E8EA23CB63C0000000000000000000000000000000000000000000000000000 (64 hex digits)
when the correct result would be:
6A9E8EA23CB63C228F31AD9268B4097F156D6AA11DCB29F14BACCFF196CE3F0AD2 (66 hex digits)
Notice that as well as the result showing '0's after B63C which you would expect it is also missing the first 6A before 9E.
While not immediately clear from the description above, a negative sign is also "silently ignored".
base_convert("-12", 10, 10)  =>  12
Note that the method can only convert max 1024 long numbers. Most people don't nee that but once I ran into an issue where binary string was converted into alphanumeric. In such case, you need to split them, convert them and join.
I wrote this as part of a college assignment to shorten urls. It works for any base and with any range of characters, eg. A-Za-z0-9 etc. Hope it helps someone. 
In order to convert base 26 (hexavigesimal) of just alphanumeric characters (A-Z), wthout integers, (as descibed at http://en.wikipedia.org/wiki/Hexavigesimal), I found this to be useful:
function base_convert_alpha( $str, $from, $to )
{
  $r = range( 'A', 'Z' );
  $clean = str_replace( $r, array_keys($r), $str );
  return base_convert( $clean, $from, $to );
}
echo base_convert_alpha( "BAC", 26, 10 );
//$clean = 102 which then returns 678
output: string(3) "314"
Convert any length bits to HEX:
function bits2hex($bin)
{
  $out = "";
  for($i=0;$i 65)   $partialCode = creaSpeedCode($resultINT,$partialCode,$BASECODE);
  return $BASECODE[$resultINT].$partialCode;
}
echo $speedCodeDEC."
"; echo $risultato = creaSpeedCode ( $speedCodeDEC,"",$BASECODE);
[EDIT BY danbrown AT php DOT net: This function provides conversion of Arabic numerals (those used by most of the world, the standard 1,2,3,4,...) into Roman numerals (I,II,III,IV,...). The original author failed to provide a description, but due to the usefulness of the function, it is being preserved from deletion, and a simple description added.] 
Extending base_convert to bases above 36, up to base 62.

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)