-
Notifications
You must be signed in to change notification settings - Fork 0
/
weibomid.html
104 lines (97 loc) · 2.67 KB
/
weibomid.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<html>
<head>
<title>Weibo mid <=> string</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
var toRadix = function (N, radix) {
// http://www.javascripter.net/faq/convert3.htm
var HexN = "", Q = Math.floor(Math.abs(N)), R;
while (true) {
R = Q % radix;
HexN = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(R) + HexN;
Q = (Q - R) / radix;
if (Q == 0) break;
}
return ((N < 0) ? "-" + HexN : HexN);
};
var toBase = function (num, base, fromBase) {
// http://forums.devnetwork.net/viewtopic.php?f=13&t=77205
var symbolSheet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/".split("");
if (typeof fromBase != "undefined") {
var decimal = 0;
for (var i = 0; i < num.toString().length; i++) {
decimal = decimal + (Math.pow(fromBase, i) * symbolSheet.toString().replace(',', '', 'g').indexOf(num.toString().charAt(num.toString().length - (i + 1))));
}
} else {
var decimal = num;
}
var conversion = "";
if (base > symbolSheet.length || base <= 1) {
return false;
}
while (decimal >= 1) {
conversion = symbolSheet[(decimal - (base * Math.floor(decimal / base)))] + conversion;
decimal = Math.floor(decimal / base);
}
return (base < 11) ? parseInt(conversion) : conversion;
};
var mid2str = function() {
var mid = $('mid').value;
var str = '';
while (mid.length > 0) {
if (mid.length < 7)
var num = parseInt(mid);
else
var num = parseInt(mid.slice(-7), 10);
str = toRadix(num, 62) + str;
if (mid.length >= 7)
mid = mid.slice(0, mid.length - 7);
else
break;
}
$('str').value = str;
};
var str2mid = function() {
var str = $('str').value;
var mid = '';
while (str.length > 0) {
if (str.length < 4)
var thisstr = str;
else
thisstr = str.slice(-4);
mid = toBase(thisstr, 10, 62) + mid;
if (str.length >= 4)
str = str.slice(0, str.length - 4);
else
break;
}
$('mid').value = mid;
};
var base10to62 = function() {
var b10 = $('base10').value;
$('base62').value = toRadix(b10, 62);
}
var base62to10 = function() {
var b62 = $('base62').value;
$('base10').value = toBase(b62, 10, 62);
}
var $ = function(id) {
return document.getElementById(id);
};
</script>
</head>
<body>
Please input in either box: <br/>
mid (number):
<input type='text' id='mid' onkeyup='mid2str();' />
string:
<input type='text' id='str' onkeyup='str2mid();' />
<hr/>
base10 number:
<input type='text' id='base10' onkeyup='base10to62();' />
base62 string:
<input type='text' id='base62' onkeyup='base62to10();' />
<hr/>
<p>©CAQ v0.1 2011/12</p>
</body>
</html>