0

I'm generating a reference number which is a number to be entered by a user. It can be around 30 digits.

I'm looking for a way to shorten the value to be entered by the user but I need to be able to convert it back to its original number.

Currently base36 turns

999999999999999999999999999999 (30 digits)
2OY99WNKL1A848GWKS0G (20 characters)

Id' probably hyphen this out to 2OY99-WNKL1-A848G-WKS0G

dsolimano
  • 8,870
  • 3
  • 48
  • 63
CathalMF
  • 9,705
  • 6
  • 70
  • 106
  • I assume the result is case insensitive and doesnt allow special characters like á but just ASCII? – T_D Aug 15 '14 at 08:36

1 Answers1

2

Assuming you're looking at ints as input, you can obviously change to base 36 (10 alpha+ 26 chars) giving you your result.

Other than that, you could allow upper/lower, widening your base to 10+52.

If you allow all ascii (might not be pretty, but should work), you could potentially use 256, so you might cut down to around 15.

I'd probably look at choosing a 100 chars i'm happy with, and simply taking every 2 numbers and converting into a char in that range.

Here's some linqpad code that will spit 144 printable(ish) chars:

Array values = Enum.GetValues(typeof(ConsoleKey));//.Dump("all values");
var val_list = values.Cast<ConsoleKey>();
var verbose = from value in val_list
    select new {IntVal = (int) value, 
                IntValPrintsAs = (char)value,
                Value=((ConsoleKey) value), 
                Straight = value
                };
verbose.Dump(); 

If you ignore some of the obvious ones like backspace, it might give you a direction :)

Noctis
  • 11,507
  • 3
  • 43
  • 82
  • Yes, I think encoding each pair of numeric digits to one of a set of 100 characters is a good idea. It would require (int)((N+1)/2) characters for each number consisting of N digits. However, Unicode characters are 16 bits (in memory) so... – Matthew Watson Aug 15 '14 at 08:42
  • ln(999999999999999999999999999999) = 69.077 . So assuming 70 bits, you can mask it, and use about 9 bytes to display that as well? – Noctis Aug 15 '14 at 08:46
  • argh, having a "duh" moment. yep, log with no base != ln, which is log(e)10 (so, 2.7), so it'll be bigger than my prev calc. It's 99.66 so about 100 bits, divide by 8 -> 12.5 --> drums please ...13 bytes in total :) (and i've been schooled) nice. – Noctis Aug 15 '14 at 09:38