我正在考虑在这里使用编码/解码技术(编码到基础36 /从基础36解码在Ruby中很简单)
how to implement a short url like urls in twitter?
想要跟踪用户推介,邀请网址。我可以使用Rails进行解码,但有没有办法用Javascript或jQuery进行编码?
答案 0 :(得分:42)
toString
上的Number
方法有一个可选参数radix:
(128482).toString(36);
128482..toString(36);
128482 .toString(36);
var num = 128482; num.toString(36);
注意这不起作用,因为数字期望一段时间后的十进制数字,而不是字母:
128482.toString(36); // Syntax error
此外,您也可以使用JS进行解码:
parseInt("2r4y", 36);
答案 1 :(得分:5)
对于任何想要在base36中编码字符串的人(因为这个问题,How do i convert string to base36 in javascript,在此重定向) -
这就是我想出来的。
/* encode / decode strings to / from base36
based on: http://snipplr.com/view/12653/
*/
var base36 = {
encode: function (str) {
return Array.prototype.map.call(str, function (c) {
return c.charCodeAt(0).toString(36);
}).join("");
},
decode: function (str) {
//assumes one character base36 strings have been zero padded by encodeAscii
var chunked = [];
for (var i = 0; i < str.length; i = i + 2) {
chunked[i] = String.fromCharCode(parseInt(str[i] + str[i + 1], 36));
}
return chunked.join("");
},
encodeAscii: function (str) {
return Array.prototype.map.call(str, function (c) {
var b36 = base36.encode(c, "");
if (b36.length === 1) {
b36 = "0" + b36;
}
return b36;
}).join("")
},
decodeAscii: function (str) {
//ignores special characters/seperators if they're included
return str.replace(/[a-z0-9]{2}/gi, function (s) {
return base36.decode(s);
})
}
};
var foo = "a-Az-Z 0-9 !@#$%^&*()-_=+[{]};:',<.>/?`~";
var bar = base36.encodeAscii(foo);
console.log(foo);
console.log(base36.decode(bar));
console.log('');
var bar = "==/" + bar + "\\==";
console.log(bar)
console.log(base36.decodeAscii(bar));
//doesn't work
console.log('');
var myString = "some string";
var myNum = parseInt(myString, 36);
console.log(myNum.toString(36))
myString = "FooBarW000t";
myNum = parseInt(myString, 36);
console.log(myNum.toString(36))
myString = "aAzZ09!@#$%^&*()-_=+[{]};:',<.>/?`~";
myNum = parseInt(myString, 36);
console.log(myNum.toString(36))
/*
Outputs:
a-Az-Z 0-9 !@#$%^&*()-_=+[{]};:',<.>/?`~
a-Az-Z 0-9 !@#$%^&*()-_=+[{]};:',<.>/?`~
==/2p191t3e192i0w1c191l0w0x1s0z10112m12161415192n1p172j3f2l3h1n1m13181o1a1q1b1r2o3i\==
==/a-Az-Z 0-9 !@#$%^&*()-_=+[{]};:',<.>/?`~\==
some
foobarw000w
aazz09
*/
答案 2 :(得分:1)
对于任何希望在python中解码@imjosh答案的人(例如,如果您已经编码了客户端并且需要解码服务器端),这就是我所使用的。我本来会在@imjosh的回答中留下评论,但评论的格式不太好。
def decodeBase36(str):
decoded_str = ""
for i in range(0, len(str), 2):
char = chr(int(str[i:i+2], 36))
decoded_str += char
return decoded_str
和不太出色的Objective-C版本:
+ (NSString *)b36DecodeString:(NSString *)b36String
{
NSMutableString *decodedString = [NSMutableString stringWithFormat:@""];
for (int i = 0; i < [b36String length]; i+=2) {
NSString *b36Char = [b36String substringWithRange:NSMakeRange(i, 2)];
int asciiCode = 0;
for (int j = 0; j < 2; j++) {
int v = [b36Char characterAtIndex:j];
asciiCode += ((v < 65) ? (v - 48) : (v - 97 + 10)) * (int)pow(36, 1 - j);
}
[decodedString appendString:[NSString stringWithFormat:@"%c", asciiCode]];
}
return decodedString;
}
答案 3 :(得分:0)
我写了这个baseXY缩短器,旨在解决我遇到的一个非常相似的问题/需求:
https://github.com/marko-36/base29-shortener。
就是这样,希望对您有所帮助:
const c=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '2', '3', '4', '5', '6', '7', '8', '9'];
//characters for encoding
function to29(i){
var sLen = Math.floor(Math.log(i)/Math.log(c.length)) +1;
var s = '';
for(ex=sLen-1; ex>-1; --ex){
s += c[Math.floor(i / Math.pow(c.length,ex))];
i = [i % Math.pow(c.length,ex)];
}
return s;
}
function from29(s){
var i = 0;
for (ex=0; ex<s.length; ++ex){
i += c.indexOf(s.substring(ex,ex+1))*Math.pow(c.length,s.length-1-ex);
}
return i;
}