URL Shortening URL 压缩算法

JAVA Byte 

  • Byte data type is an 8-bit signed two's complement integer.

  • Minimum value is -128 (-2^7)

  • value is 127 (inclusive)(2^7 -1)

  • To calculate the 2's complement of an integer, invert the binary equivalent of the number by changing all of the ones to zeroes and all of the zeroes to ones (also called 1's complement), and then add one.



How to Get MD5 in Java


	public static String MD5Hex(String message) {
		String res = null;
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			res = convertByteArrayToHexString(md.digest(message.getBytes()));
		} catch (Exception ex) {
		}
		return res;
	}
	


How to Convert to Hex MD5 

	public static String convertByteArrayToHexString(byte[] b) {
		StringBuffer resSB = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			//System.out.print(Integer.valueOf(b[i])+",");
			
			/*if(i % 4 == 0) 
				System.out.print(",");
			System.out.print(Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1));
			*/
			resSB.append(Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1));
		}

		return resSB.toString();
	}

Explanation of Mathematical Transformation 

	 /*    ||
	 *     ||   (-47)10 = (..1111111010001)2                  
	 *     ||    
	 *     ||   (ff)16 = (1111 1111)2
	 *     ||
	 *     ||   -47 & 0xff = (1101 0001)2 = d1(16)
	 *     ||    
	 *     ||    d1 & 0x100 = 0x1d1 in case of 0 length string
	 *     ||    
	 *     ||    0x1d1 .subString(1) = d1 
	 *     \/ 
	 *     
	*/

How to Convert to Hex MD5  

        /*    1. MD5(http://www.leetcode.com + ExtraMessageToMakeTheResultUnique) 
	 *  	
	 *    2. -47,-121,94,25,-112,-116,-41,-35,-119,15,48,12,-23,83,-40,37
	 *  
	 *     
			 -47(11111111111111111111111111010001), 209(11010001) d1,0x100,1d1,d1
			-121(11111111111111111111111110000111), 135(10000111) 87,0x100,187,87
			  94(                         1011110),  94( 1011110) 5e,0x100,15e,5e
			  25(                           11001),  25(   11001) 19,0x100,119,19
			-112(11111111111111111111111110010000), 144(10010000) 90,0x100,190,90
			-116(11111111111111111111111110001100), 140(10001100) 8c,0x100,18c,8c
			 -41(11111111111111111111111111010111), 215(11010111) d7,0x100,1d7,d7
			 -35(11111111111111111111111111011101), 221(11011101) dd,0x100,1dd,dd
			-119(11111111111111111111111110001001), 137(10001001) 89,0x100,189,89
			  15(                            1111),  15(    1111)  f,0x100,10f,0f
			  48(                          110000),  48(  110000) 30,0x100,130,30
			  12(                            1100),  12(    1100)  c,0x100,10c,0c
			 -23(11111111111111111111111111101001), 233(11101001) e9,0x100,1e9,e9
			  83(                         1010011),  83( 1010011) 53,0x100,153,53
			 -40(11111111111111111111111111011000), 216(11011000) d8,0x100,1d8,d8
			  37(                          100101),  37(  100101) 25,0x100,125,25
	 *     
	 *    3. d1,87,5e,19,90,8c,d7,dd,89,0f,30,0c,e9,53,d8,25
	*/


Java Get MD5

Bit Manipulation

How to Split and Map MD5 HexString to a predefined 62 Chars Set


	/*  d1,87,5e,19,90,8c,d7,dd,89,0f,30,0c,e9,53,d8,25 
	 *     ||
	 *     ||
	 *  d1875e19,908cd7dd,890f300c,e953d825
	 *     ||
	 *     \/
	 *  d1875e19 & 3FFFFFFF =  11010001100001110101111000011001
	 *                      &  00111111111111111111111111111111
	 *  294084121           =    010001100001110101111000011001
	 *  294084121 &      3D =    010001100001110101111000011001
	 *                      &                          00111101
	 *                      =                             11001  =  25
	 *  294084121 >>      5 =                           9190128    
	 *  9190128   &      3D =      1001000110010000000100101000
	 *                      &                          00111101
	 *                      =                            101000  =  48
	 *  9190128   >>      5 =                            287191
	 *  287191    &      3D                                      =  21                         
	 *  287191    >>      5 =                              8974
	 *  8974      &      3D                                      =  12
	 *  8974      >>      5 =     							280
	 *  280       &      3D                                      =  24
	 *  280       >>      5 =     							  8
	 *  8         &      3D                                      =   8
	 */

	public static String[] _64_base_6_dit_miniURL (String message) {
		String key = "ExtraMessageToMakeTheResultUnique"; 
		String charSet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
		String hexStr = MD5Hex(key + message); //  length = 128 bits = 16 bytes = 32 hex digits
		String[] miniURL = new String[4];

		for (int i = 0; i < 4; i++) {//four miniURL in case of collision 
			char[] tmp = new char[6];
			String subHex = hexStr.substring(i * 8, (i+1) * 8);
			long idx = Long.valueOf("3FFFFFFF", 16) & Long.valueOf(subHex, 16);

			for (int k = 0; k < 6; k++) {
				int index = (int) (Long.valueOf("3D", 16) & idx);
				tmp[k] = charSet.charAt(index);
				idx = idx >> 5;
			}
			miniURL[i] = new String(tmp);
		}

		return miniURL;
	}</span>


Interesting discussions on this topic

A funny solution from GeektoGeek, don't why it ranks the highest from Google Search Results.



A Much Simpler 62base miniURL Generator 

This approach apparently doesn't guarantee unique answers, but It is a good way to grab the idea quickly. Unfortunately it was too late when I got this one. But the idea of 62-based miniURL algorithm is pretty straight forward. It is basically a map function between the original long URL and the shortened URL. 

        static int baseNum = 62;
        private static readonly String baseDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        public static string Base62ToString(long fromValue)
        {
            string toValue = fromValue == 0 ? "0" : "";
            int mod = 0;
 
            while (fromValue != 0)
            {
                mod = (int)(fromValue % baseNum); //should be safe
                toValue = baseDigits.Substring(mod, 1) + toValue;
                fromValue = fromValue / baseNum;
            }
 
            return toValue;
        }


Base 62 Explained

Finally the blog helped me to understand this question

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值