1

I have a structure that I am converting to a byte array of length 37, then to a string from that.

I am writing a very basic activation type library, and this string will be passed between people. So I want to shorten it from length 37 to something more manageable to type.

Right now:

  • Convert the structure to a byte array,
  • Convert the byte array to a base 64 string (which is still too long).

What is a good way to shorten this string, yet still maintain the data stored in it?

Thanks.

5
  • Could you give some examples? Commented Dec 28, 2011 at 17:36
  • Which text encoding are you using? Commented Dec 28, 2011 at 17:40
  • I am not using any text encoding, I wanted to avoid weird characters. Commented Dec 28, 2011 at 17:42
  • @TyRozak - Using Base64 would avoid "wierd" Characters. If you want a shorter string make the structure smaller. Its not clear what you are trying to do. Commented Dec 28, 2011 at 17:44
  • Unfortunately, the structure is already minimal. So it is looking like this won't be possible Commented Dec 28, 2011 at 17:47

6 Answers 6

2

In the general case, going from an arbitrary byte[] to a string requires more data, since we assume we want to avoid non-printable characters. The only way to reduce it is to compress before the base-whatever (you can get a little higher than base-64, but not much - and it certainly isn't any more "friendly") - but compression won't really kick in for such a short size. Basically, you can't do that. You are trying to fit a quart in a pint pot, and that doesn't work.

You may have to rethink your requirements. Perhaps save the BLOB internally, and issue a shorter token (maybe 10 chars, maybe a guid) that is a key to the actual BLOB.

Sign up to request clarification or add additional context in comments.

Comments

0

Data compression may be a possiblity to check out, but you can't just compress a 40-byte message to 6 bytes (for example).

If the space of possible strings/types is limited, map them to a list (information coding).

Comments

0

I don't know of anything better than base-64 if you actually have to pass the value around and if users have to type it in.

If you have a central data store they can all access, you could just give them the ID of the row where you saved it. This of course depends on how "secret" this data needs to be.

But I suspect that if you're trying to use this for activation, you need them to have an actual value.

How will the string be passed? Can you expect users to perhaps just copy/paste? Maybe some time spent on clearing up superfluous line breaks that come from an email reader or even your "Copy from here" and "Copy to here" lines might bear more fruit!

Comments

0

Can the characters in your string have non-printable chars? If so, you don't need to base64-encode the bytes, you can simply create the string from them (saved 33%)

string str = new string(byteArray.Cast<char>().ToArray());

Also, are the values in the byte array restricted somehow? If they fall into a certain range (i.e., not all of the 256 possible values), you can consider stuffing two of each in each character of the string.

Comments

0

If you really have 37 bytes of non-redundant information, then you are out of luck. Compression may help in some cases, but if this is an activation key, I would recommend having keys of same length (and compression will not enforce this).

If this code is going to be passed over e-mail, then I see no problem in having an even larger key. Another option might be to insert hyphens every 5-or-so characters, to break it into smaller chunks (e.g. XXXXX-XXXXX-XXXXX-XXXXX-XXXXX).

Comments

0

Use a 160bit hash and hope no collisions? It would be much shorter. If you can use a look-up table, just use a 128 or even 64bit incremental value. Much much shorter than your 37 chars.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.