0

Lets say I have something like 97463

I want to code it into letters 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)

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
David19801
  • 11,214
  • 25
  • 84
  • 127

2 Answers2

2

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

Dogbert
  • 212,659
  • 41
  • 396
  • 397
0

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
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194