How do I get a unique 8 character string from a 24 character string?
NickName:Joshua Ask DateTime:2013-03-01T06:20:43

How do I get a unique 8 character string from a 24 character string?

I would like to get a 8 character string from a unique 24 character string so as to make a shortened URL. The 8 character string HAS to be based on the unique 24 character string. If I make a random 8 character string then there will have to be a database lookup to see it's not already taken. Also I don't want to use the first 8 characters or last 8 characters of the 24 character string. The 24 characters are a MongoDB Object Id. Thanks.

Copyright Notice:Content Author:「Joshua」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/15146811/how-do-i-get-a-unique-8-character-string-from-a-24-character-string

Answers
Marc B 2013-02-28T22:34:33

Assuming your 24 char string is upper+lower alpha-numeric, that'd be\n\n26+26+10 chars = 62 chars = 6 bits required to present them, and\n24 * 6 = 144 bits to store them.\n\n\n144bits/8bits/byte = 18 bytes. you cannot compress your 24 chars into 8 characters without having a potential collision. 8 chars at 8bits/char = 64bits maximum.",


zerkms 2013-02-28T22:26:47

8 is too few.\n\nHere is how you can get 16 from 24:\n\n$id = '507f191e810c19729de860ea';\n\n$str = base64_encode(implode('', array_map(function($c) { return chr(hexdec($c)); }, str_split($id, 2))));\n\nvar_dump($str); // UH8ZHoEMGXKd6GDq\n\n\nFor less characters you need to have bigger character base, but the thing is - there are no url safe left.",


More about “How do I get a unique 8 character string from a 24 character string?” related questions

How do I get a unique 8 character string from a 24 character string?

I would like to get a 8 character string from a unique 24 character string so as to make a shortened URL. The 8 character string HAS to be based on the unique 24 character string. If I make a rando...

Show Detail

Obtain a 16 character string from a 32 character string

How do i obtain a string that is 16 characters long, from a string that is 32 characters long. This 32 character string is a md5 hash. So how do i get the obtained 16 character string to be reasona...

Show Detail

How to remove character from string?

I have a string which is getting from a userInput. What I want to do now is removing a unique character from this string but only remove it once. The main problem is that this unique character does...

Show Detail

Generate Unique 6 Character alphanumeric string from a 10 Character alphanumeric string

As the title suggests im trying to generate a unique 6 Character alphanumeric string from a 10 Character alphanumeric string. Is there any algorithms people have used / can suggest for this? I usin...

Show Detail

Creating a unique 8 character string from a sequential integer

I need to generate a unique 8 character string from a sequential integer (0, 1, 2, 3, etc). I tried hashing the int with md5/sha256/sha512 and then shortening it to 8 characters but there are quit...

Show Detail

Generate unique 5 character string?

How can I generate a UNIQUE (not random) 5 character string in javascript. I do not want to use Math.random() as it does not ensure uniqueness. It should contain characters from a-z,A-Z and 0-9.

Show Detail

Retrieve positions of unique characters in character string in R

I'm trying to track sampling effort for pollen counts. My group is trying to see how many different pollen species we count compared to total number of grains counted. These are similar to rarefa...

Show Detail

How to generate 8 character unique string without checking if it exist in the database

I want to generate 1 million qr codes every day. every qr code should have random unique value. in order to achieve uniqueness i have two options as below 1)First option is to generate 8 characters

Show Detail

Splitting a string by newline character succeeds but removes the last character from the string

I am trying to split a string by newline character (\n) but it removes the last character from the string. How do I rectify this? I have tried replacing the newline character with a pair of the same

Show Detail

How to get the first unique character?

The below is my code. The question is to find the first unique(The not repeated) character in the given string, I am a beginner to the Java, so right now I am wondering by how to debug the code.......

Show Detail