开发者

code a number into letters

开发者 https://www.devze.com 2023-02-06 05:38 出处:网络
Lets say I have something like 97463 I want to code it into letter开发者_JAVA百科s say kljhs I\'m using php/javascript at the moment, but I guess its a universal problem.

Lets say I have something like 97463

I want to code it into letter开发者_JAVA百科s say kljhs

I'm using php/javascript at the moment, but I guess its a universal problem.

Whats the most efficient way to do this in a way thats reversible?

(reversible meaning given numbers I can make the letter code and then later given just the number code I can return the letters)


You could just use the strtr function

$input = '123456';
$output = strtr($input, '0123456789', 'abcdefghij');

To reverse, use

$input = 'bcdefg';
$output = strtr($input, 'abcdefghij', '0123456789');

http://codepad.org/6hGqJPD6


You can use dechex() to encode the number as hexadecimal, and hexdec() to reverse:

$hex = dechex(97463); // "17cb7"
$dec = hexdec($hex);  // 97463

Alternatively you may want to use base_convert(), to convert to an arbitrary base from 2 to 36 :

$enc = base_convert(97463, 10, 36);  // "237b"
$dec = base_convert("237b", 36, 10); // 97463
0

精彩评论

暂无评论...
验证码 换一张
取 消