Created
November 22, 2018 12:28
-
-
Save cnmoro/c9cf06394e9be09071876baafe06949f to your computer and use it in GitHub Desktop.
Converter String em representação de int Semi-unique
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Fonte: Nolesh @ StackOverflow | |
public int getUniqueInteger(String name) { | |
String plaintext = name; | |
int hash = name.hashCode(); | |
MessageDigest m; | |
try { | |
m = MessageDigest.getInstance("MD5"); | |
m.reset(); | |
m.update(plaintext.getBytes()); | |
byte[] digest = m.digest(); | |
BigInteger bigInt = new BigInteger(1,digest); | |
String hashtext = bigInt.toString(10); | |
// Now we need to zero pad it if you actually want the full 32 chars. | |
while(hashtext.length() < 32 ) { | |
hashtext = "0"+hashtext; | |
} | |
int temp = 0; | |
for(int i =0; i<hashtext.length();i++) { | |
char c = hashtext.charAt(i); | |
temp+=(int)c; | |
} | |
return hash+temp; | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
} | |
return hash; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment