如题。比如我有很多字符串,有中文也有英文字母符号之类的。而且每天字符串的数量都可能有更新。如何能够给不同的字符串生成一个唯一的编码?

解决方案 »

  1.   


    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
     }}
      

  2.   

    唯一编码很容易,字符串本身就是他唯一的编码。还有诸如base64,gzip等常见编码。关键是你的需求。你要想给100w个不同字符串每个1字节长度的编码,那是做梦。
      

  3.   

    说的对。还是我问题描述的不过清楚。这么说吧,我有500万的字符串,每个字符串的最大长度是300。每个字符既可能是汉字也可能是字母符号,那么我应该如何给这些字符串生成唯一的编码。
    我希望是像java的hashcode那样的算法生成,而不是一个一个的给字符串去编号。
      

  4.   

    我贴一个类,这个可以生成类似hibernate主键生成策略中uuid 32主键序列,一般可以用来生成主键,唯不唯一自己看喽!
    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());
    }
    }