Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generate unique fixed length code from a string

 
Swapna Parimishetty
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic