如题。比如我有很多字符串,有中文也有英文字母符号之类的。而且每天字符串的数量都可能有更新。如何能够给不同的字符串生成一个唯一的编码?
解决方案 »
- java 怎么把Excel文件里的图片读取到某个指定的文件夹?
- 菜鸟问题,File类
- 如何通过 bat 文件编写批处理,然后运行一个jar文件,但不出现控制台窗口呢?
- 关于java中的报表生成及打印问题
- 我想问一下如何将class文件生成可执行文件exe
- 如何在RMI中传递一个对象?
- 我的jbuilder6.0为什么用jdk1.4升级后,编出的东东后,鼠标滚轮不能够用?
- 这里的问题有些杂乱,如果能够分分类就好了
- 谁有连接过Apache1.3.24何tomcat3.3.1
- 大家都学哪种java啊? MS java4.0 or Sun JDK 1.3
- 一道为“未成熟”的java爱好者的题,成熟者勿笑咱“低智儿” --!只为娱乐
- 求助解决不能重复积分
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;public class md5 {
public String str; public void md5s(String plainText) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes());
byte b[] = md.digest(); int i; StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
str = buf.toString();
System.out.println("result: " + buf.toString());// 32位的加密
System.out.println("result: " + buf.toString().substring(8, 24));// 16位的加密
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace(); }
} public static void main(String agrs[]) {
md5 md51 = new md5();
md51.md5s("4");//加密4
}}
我希望是像java的hashcode那样的算法生成,而不是一个一个的给字符串去编号。
package com.anxin.utils;import java.io.Serializable;
import java.net.InetAddress;/**
* 生成类似hibernate中uuid 32位主键序列
*
* @version: V1.0
*/
public class UUIDGenerator { private static final int IP; public static int IptoInt(byte[] bytes) {
int result = 0;
for (int i = 0; i < 4; i++) {
result = (result << 8) - Byte.MIN_VALUE + (int) bytes[i];
}
return result;
} static {
int ipadd;
try {
ipadd = IptoInt(InetAddress.getLocalHost().getAddress());
} catch (Exception e) {
ipadd = 0;
}
IP = ipadd;
}
private static short counter = (short) 0;
private static final int JVM = (int) (System.currentTimeMillis() >>> 8); public UUIDGenerator() {
}
public static int getJVM() {
return JVM;
}
public static short getCount() {
synchronized (UUIDGenerator.class) {
if (counter < 0)
counter = 0;
return counter++;
}
}
public static int getIP() {
return IP;
}
public static short getHiTime() {
return (short) (System.currentTimeMillis() >>> 32);
} public static int getLoTime() {
return (int) System.currentTimeMillis();
} private final static String sep = ""; public static String format(int intval) {
String formatted = Integer.toHexString(intval);
StringBuffer buf = new StringBuffer("00000000");
buf.replace(8 - formatted.length(), 8, formatted);
return buf.toString();
} public static String format(short shortval) {
String formatted = Integer.toHexString(shortval);
StringBuffer buf = new StringBuffer("0000");
buf.replace(4 - formatted.length(), 4, formatted);
return buf.toString();
} public static String generate() {
return String.valueOf(new StringBuffer(36).append(format(getIP())).append(sep)
.append(format(getJVM())).append(sep)
.append(format(getHiTime())).append(sep)
.append(format(getLoTime())).append(sep)
.append(format(getCount())).toString());
}
public static void main(String args[]){
System.out.println(UUIDGenerator.generate());
}
}