将大数字转换为字母(并再次) [英] Converting large numbers into letters (and back again)

查看:184
本文介绍了将大数字转换为字母(并再次)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一个词的存储大数字作为字母的想法?例如,假设我有(相对较小)数字138201162401719,并且我想减少字符数(我知道这对于节省磁盘空间没有帮助)到尽可能少的字符数。在英语字母表中有26个字母(但我将它们计为25,因为我们需要一个零字母)。如果我开始把我的大数分成每个25或更少的块,我得到:

Is there a term for the idea of storing large numbers as letters? For example let's say I have the (relatively small) number 138201162401719 and I want to shrink the number of characters (I know this does not help with saving disk space) to the fewest possible number of characters. There are 26 letters in the English alphabet (but i count them as 25 since we need a zero letter). If I start splitting up my large number into pieces that are each 25 or less I get:


13,8,20,11,6 ,24,0,17,19

13, 8, 20, 11, 6, 24, 0, 17, 19



如果我然后计算字母表的数字a = 0,b = 1,c = 2,d = 3 ...我可以将其转换为:

If I then count the numbers of the alphabet a=0, b=1, c=2, d=3... I can convert this to:


NIULGYART

NIULGYART

所以我从15位数字(138201162401719)到9个字符长(NIULGYART)。

So I went from 15 digits long (138201162401719) to 9 characters long (NIULGYART). This could of course be easily converted back to the original number as well.

所以...我的第一个问题是这是否有一个名称和我的第二个有没有可以进行转换的PHP代码(双向)?

So...my first question is "Does this have a name" and my second "Does anyone have PHP code that will do the conversion (in both directions)?"

术语,使我可以在Google做我自己的研究......虽然工作代码示例也很酷。

I am looking for proper terminology so that I can do my own research in Google...though working code examples are cool too.

推荐答案

这只有在您考虑在以字符串处理之前存储您的号码时才可能。因为你不能存储巨大的数字作为整数。您将丢失精度( 13820116240171986468445 将存储为 1.3820116240172E + 22 ),因此大量数字丢失。

This only possible if you're considering to store your number before processing as a string. Because you can't store huge number as integers. You will lost the precision (13820116240171986468445 will be stored as 1.3820116240172E+22) so the alot of digits are lost.

如果你考虑将数字存储为字符串,这将是你的答案:

If you're considering storing the number as a string this will be your answer:

intval chr preg_match_all

<?php

$regex = '/(2[0-5])|(1[0-9])|([0-9])/'; 
$numberString = '138201162401719'; 

preg_match_all($regex, $numberString, $numberArray, PREG_SET_ORDER);

echo($numberString . " -> ");

foreach($numberArray as $value){
    $character = chr (intval($value[0]) + 65);
    echo($character);
}

?>

演示

这是结果:

138201162401719 - > NIULGYART

138201162401719 -> NIULGYART

这篇关于将大数字转换为字母(并再次)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆