• java随机字符串+校验位


    package com.g4m;
    
    import java.util.Stack;  
    
    
    public class JingzhiTest {  
    	private final static String _str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";  
    	private static char[] charSet = _str.toCharArray();    
        private static int len = _str.length(); 
        private static int checkNum = 10; 
        
    	/** 
    	 * 将10进制转化为62进制  
    	 * @param number  
    	 * @return 
    	 */  
    	public static String numTo62String(long number){  
    		Long rest=number;  
    		Stack<Character> stack=new Stack<Character>();  
    		StringBuilder result=new StringBuilder(0);  
    		while(rest!=0){  
    			stack.add(charSet[new Long((rest-(rest/62)*62)).intValue()]);  
    			rest=rest/62;  
    		}  
    		for(;!stack.isEmpty();){  
    			result.append(stack.pop());  
    		}  
    		return result.toString();  
    	}  
        
    	/**
    	 * 生成长度为length的字符串,length > 1
    	 * @param length
    	 * @return
    	 */
    	public static String createRandom(int length) {
    		if(length < 2) {
    			return "";
    		}
    		StringBuffer str = new StringBuffer();
    		for (int i = 0; i < length-1; i++) {
    			int a = (int) (Math.random()*62); //0-1,等于0,不等于1
    			str.append(charSet[a]);
    		}
    		return str.toString() + getCheckCode(str.toString());
    	}
    	/**
    	 * 生成长度为length的字符串,length > 1
    	 * @param length
    	 * @return
    	 */
    	public static String createStringById(long id,int length) {
    		String strNum = numTo62String(id);
    		StringBuffer str = new StringBuffer();
    		for (int i = strNum.length(); i < length-1; i++) {
    			str.append(0);
    		}
    		return getCheckCode(str.toString()+strNum)+str.toString()+strNum;
    	}
    	/**
    	 * 生成长度为length的字符串,length > 1
    	 * @param length
    	 * @return
    	 */
    	public static String createStringById(long id) {
    		return createStringById(id,6);
    	}
    	
    	/**
    	 * 添加校验位
    	 * @param string
    	 * @return
    	 */
        private static char getCheckCode(String string) {
        	long unm = toNum(string)+checkNum;
    		int a = (int) (unm%62);
    		return charSet[a];
    	}
    
        /**
         * 字符串转数字
         * @param s
         * @return
         */
    	public static long toNum(String s) {  
            int a = s.length()-1;  
            long val=0;  
            for(int i=a;i>=0;i--){  
                char c = s.charAt(i);  
                val += (_str.indexOf(c)*Math.pow(len, a-i));  
            }  
            return val;  
        }  
    	
    	
    	/**
    	 * 校验短链是否正确
    	 * @param link
    	 * @return
    	 */
    	public static boolean checkShortLink(String link) {
    		if(link==null) {
    			return false;
    		}
    		if(link.length()<2) {
    			return true;
    		}
    		String substring = link.substring(1, link.length());
    		char charAt = link.charAt(0);
    		char checkCode = getCheckCode(substring);
    		if((int)charAt == (int)(checkCode)) {
    			return true;
    		}
    		return false;
    	}
    	
    	/** 
    	 * @param args 
    	 */  
    	public static void main(String[] args) {
    		String createStringById = createStringById(1l);
    		System.out.println("id:"+createStringById);
    		
    		boolean checkShortLink = checkShortLink(createStringById);
    		System.out.println("check:"+checkShortLink);
    		
    		//生成随机字符串
    		String createRandom = createRandom(6);
    		System.out.println(createRandom);
    		
    		//校验随机字符串
    		boolean checkShortLink1 = checkShortLink("YB0mEmm");
    		System.out.println(checkShortLink1);
    	}
    }
    

      

  • 相关阅读:
    MSDN Visual系列:利用关联来过滤MOSS中的BDC数据
    SharePoint2007中开箱即用的权限(策略)级别
    SharePoint命令提示符
    使自定义的aspx页面仅用于部分SharePoint站点
    SharePoint 2010 中的站点模板(Site Templates)
    MSDN Visual系列:MOSS企业级搜索之三——创建和使用搜索范围
    推荐《Office SharePoint Server 2007案例实战开发》
    MSDN Visual系列:MOSS企业级搜索之四——创建内容源用于爬网业务数据
    通过主机标头实现多个SharePoint Web应用程序共用一个端口
    MSDN Visual系列:在MOSS中创建一个BDC实体
  • 原文地址:https://www.cnblogs.com/go4mi/p/10310647.html
Copyright © 2020-2023  润新知