URL Shortening URL 压缩算法
2016-01-06 00:37
585 查看
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
[b]MD5 in Java[/b]
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
[b]MD5 [/b]
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
Binary to Decimal to Hex
Interesting discussions on this topic
点击打开链接
A funny solution from GeektoGeek, don't why it ranks the highest from Google Search Results.
点击打开链接
Decimal/Two’s Complement Converter
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.
Base 62 Explained
点击打开链接
Finally the blog helped me to understand this question
点击打开链接
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
[b]MD5 in Java[/b]
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
[b]MD5 [/b]
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) 4000 { 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>
Binary to Decimal to Hex
Interesting discussions on this topic
点击打开链接
A funny solution from GeektoGeek, don't why it ranks the highest from Google Search Results.
点击打开链接
Decimal/Two’s Complement Converter
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
点击打开链接
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c语言实现hashmap(转载)
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序