A java question: how to create the uniq ID of a string

Put your problem here if it does not fit any of the other categories.

A java question: how to create the uniq ID of a string

Postby cabernet1976 » Tue Jan 15, 2008 6:26 pm

I am real a novice in Java :oops:
Could somebody tell me how to create a uniq ID of a string?
The requirment is: different string will get different uniq ID, the uniq ID is int type.
Please tell me if I haven't describe it clear.
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Top

Re: A java question: how to create the uniq ID of a string

Postby plusminus » Wed Jan 16, 2008 12:34 am

Hello cabernet1976,

you said:
cabernet1976 wrote:The requirement is: different string will get different uniq ID

So it is ok, if Strings with same content get the same 'ID's :?:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                  String myString = "Hello anddev.org";
  2.                  int id = myString.hashCode();
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


or some advanced hashing:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 try {
  2.                         MessageDigest digest = MessageDigest.getInstance("MD5");
  3.                     digest.update(myString.getBytes());
  4.                     byte[] hash = digest.digest();
  5.                 } catch (NoSuchAlgorithmException e) {
  6.                         e.printStackTrace();
  7.                 }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Let us know if it helped you :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby cabernet1976 » Wed Jan 16, 2008 2:19 am

Thank you, plusminus, you are so warmhearted.
I will use hashCode.
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Postby plusminus » Wed Jan 16, 2008 5:20 pm

:oops:
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: MSNbot Media and 15 guests