wood burning stoves 2.0*
The moose likes Java in General and the fly likes generate unique fixed length code from a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "generate unique fixed length code from a string" Watch "generate unique fixed length code from a string" New topic
Author

generate unique fixed length code from a string

Swapna Parimishetty
Greenhorn

Joined: Sep 17, 2009
Posts: 5
hi all

I have a unique string obtained by combining 2 values

>>id(unique)
>>subject(sometimes not unique , variable length)

i have a logic where iam getting a unique code whose length is proportional to subject, but i want a fixed length code

So my requirement is to encode this string to obtain a unique code(fixed length 16 letter) using java

in order to do this what algorithm or logic i need to follow

help is appreciated...

thanks in advance
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42913
    
  68
Welcome to JavaRanch.

If the ID is already unique, then you could just append the subject to it, and truncate the result to 16 characters, no? If the result is less than 16 characters, pad it with some filler character (like spaces).
Swapna Parimishetty
Greenhorn

Joined: Sep 17, 2009
Posts: 5
Ulf Dittmer wrote:Welcome to JavaRanch.

If the ID is already unique, then you could just append the subject to it, and truncate the result to 16 characters, no? If the result is less than 16 characters, pad it with some filler character (like spaces).



thanks alot for your reply

my encoding logic generates 24 letter unique code(including special symbols) but i want only 16 letter code(containing 0-9 and A-Z)

is there any way to get 16 letter unique code(containing 0-9 and A-Z) for 16 letter unique input string

thanks once again
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42913
    
  68
If you already have a unique 16 letter input string, how would a "16 letter unique code" be any different? Can you give an example of both, so that we know what the result should be?
Swapna Parimishetty
Greenhorn

Joined: Sep 17, 2009
Posts: 5
Ulf Dittmer wrote:If you already have a unique 16 letter input string, how would a "16 letter unique code" be any different? Can you give an example of both, so that we know what the result should be?


You are correct but i dont want a meaningful unique code

i need a encoded string with (0-9 and A-Z) characters only

thanks for your immediate reply..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42913
    
  68
Does the code need to be cryptographically secure? If not, you could just run the 16 letter string you have through something like ROT-13.
Swapna Parimishetty
Greenhorn

Joined: Sep 17, 2009
Posts: 5
Ulf Dittmer wrote:Does the code need to be cryptographically secure? If not, you could just run the 16 letter string you have through something like ROT-13.


there is no need of the code to be secured cryptographically

but i want the unique code containing just 0-9 and A-Z
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Either generate a more helpful ID from the beginning, or use a known, reversible transformation to move between the non-numeric characters to alpha-numeric.

You might just be able to use a hashing algorithm and convert the result to base 36.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

Back to the original question... I don't think that this is possible.

If one of the inputs is a variable length string, that can be any size, then there are unlimited number of possibilities. Now, if you want your code to be unique, then this also needs to be an unlimited number of possibilities -- which isn't possible if the code is restricted to a fix length.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: generate unique fixed length code from a string