Is it possible to base 36 encode with JavaScript / jQuery?
I'm thinking about using the encode/decode technique here (Encoding to base 36/decoding from base 36 is simple in Ruby)
how to implement a short url like urls in twitter?
Idea being to track user referrals, invite URLs. I can use Rails to decode, but it there a way to encode with Javascript or jQuery?
javascript jquery
add a comment |
I'm thinking about using the encode/decode technique here (Encoding to base 36/decoding from base 36 is simple in Ruby)
how to implement a short url like urls in twitter?
Idea being to track user referrals, invite URLs. I can use Rails to decode, but it there a way to encode with Javascript or jQuery?
javascript jquery
add a comment |
I'm thinking about using the encode/decode technique here (Encoding to base 36/decoding from base 36 is simple in Ruby)
how to implement a short url like urls in twitter?
Idea being to track user referrals, invite URLs. I can use Rails to decode, but it there a way to encode with Javascript or jQuery?
javascript jquery
I'm thinking about using the encode/decode technique here (Encoding to base 36/decoding from base 36 is simple in Ruby)
how to implement a short url like urls in twitter?
Idea being to track user referrals, invite URLs. I can use Rails to decode, but it there a way to encode with Javascript or jQuery?
javascript jquery
javascript jquery
edited Jul 4 '17 at 15:14
guaka
10.6k74583
10.6k74583
asked Mar 3 '12 at 2:00
AnApprenticeAnApprentice
38.2k155497886
38.2k155497886
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
The toString
method on Number
has an optional argument of radix:
(128482).toString(36);
128482..toString(36);
128482 .toString(36);
var num = 128482; num.toString(36);
Note this doesn't work, because numbers expect decimal digits after a period, not letters:
128482.toString(36); // Syntax error
Also, you can decode with JS as well:
parseInt("2r4y", 36);
2
Free DEMO: jsfiddle.net/ewsmJ
– paislee
Mar 3 '12 at 2:06
7
As for why128482.toString(36)
doesn't work, but128482..toString(36)
(note the two dots) does...JS likes to snarf the first dot for the number whenever it can, and128482.
is a valid number literal, even without anything after the decimal point.
– cHao
Mar 3 '12 at 2:14
1
* Possible loss of accuracy for numbers greater than 2^53
– c01nd01r
Feb 13 '18 at 9:14
This is good, But if I want to remove look-alike characters (1-l or 0-O) what can I do? and for working with long numbers?
– QMaster
May 23 '18 at 20:27
add a comment |
For anyone looking for how to encode a string in base36 (since this question, How do i convert string to base36 in javascript , is redirected here) -
Here's what I came up with.
/* 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
*/
What kind of strings are accepted? A string can be anything.
– Maarten Bodewes
Jan 27 at 16:12
Also: this seems to always use 2 characters per character. This is great, but it doesn't provide any length advantages; you might as well use hexadecimals.
– Maarten Bodewes
Jan 27 at 16:23
add a comment |
For anyone looking to decode @imjosh's answer in python (say if you've encoded client-side and need to decode server-side), this is what I used. I would have left as a comment in @imjosh's answer but comments don't format very well.
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
and a not-as-elegant Objective-C version:
+ (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;
}
Please specify what the function does and what kind of inputs are expected. As it is, this is a code only answer.
– Maarten Bodewes
Jan 27 at 16:13
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9542726%2fis-it-possible-to-base-36-encode-with-javascript-jquery%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The toString
method on Number
has an optional argument of radix:
(128482).toString(36);
128482..toString(36);
128482 .toString(36);
var num = 128482; num.toString(36);
Note this doesn't work, because numbers expect decimal digits after a period, not letters:
128482.toString(36); // Syntax error
Also, you can decode with JS as well:
parseInt("2r4y", 36);
2
Free DEMO: jsfiddle.net/ewsmJ
– paislee
Mar 3 '12 at 2:06
7
As for why128482.toString(36)
doesn't work, but128482..toString(36)
(note the two dots) does...JS likes to snarf the first dot for the number whenever it can, and128482.
is a valid number literal, even without anything after the decimal point.
– cHao
Mar 3 '12 at 2:14
1
* Possible loss of accuracy for numbers greater than 2^53
– c01nd01r
Feb 13 '18 at 9:14
This is good, But if I want to remove look-alike characters (1-l or 0-O) what can I do? and for working with long numbers?
– QMaster
May 23 '18 at 20:27
add a comment |
The toString
method on Number
has an optional argument of radix:
(128482).toString(36);
128482..toString(36);
128482 .toString(36);
var num = 128482; num.toString(36);
Note this doesn't work, because numbers expect decimal digits after a period, not letters:
128482.toString(36); // Syntax error
Also, you can decode with JS as well:
parseInt("2r4y", 36);
2
Free DEMO: jsfiddle.net/ewsmJ
– paislee
Mar 3 '12 at 2:06
7
As for why128482.toString(36)
doesn't work, but128482..toString(36)
(note the two dots) does...JS likes to snarf the first dot for the number whenever it can, and128482.
is a valid number literal, even without anything after the decimal point.
– cHao
Mar 3 '12 at 2:14
1
* Possible loss of accuracy for numbers greater than 2^53
– c01nd01r
Feb 13 '18 at 9:14
This is good, But if I want to remove look-alike characters (1-l or 0-O) what can I do? and for working with long numbers?
– QMaster
May 23 '18 at 20:27
add a comment |
The toString
method on Number
has an optional argument of radix:
(128482).toString(36);
128482..toString(36);
128482 .toString(36);
var num = 128482; num.toString(36);
Note this doesn't work, because numbers expect decimal digits after a period, not letters:
128482.toString(36); // Syntax error
Also, you can decode with JS as well:
parseInt("2r4y", 36);
The toString
method on Number
has an optional argument of radix:
(128482).toString(36);
128482..toString(36);
128482 .toString(36);
var num = 128482; num.toString(36);
Note this doesn't work, because numbers expect decimal digits after a period, not letters:
128482.toString(36); // Syntax error
Also, you can decode with JS as well:
parseInt("2r4y", 36);
edited Mar 3 '12 at 2:13
answered Mar 3 '12 at 2:05
AmadanAmadan
130k13143195
130k13143195
2
Free DEMO: jsfiddle.net/ewsmJ
– paislee
Mar 3 '12 at 2:06
7
As for why128482.toString(36)
doesn't work, but128482..toString(36)
(note the two dots) does...JS likes to snarf the first dot for the number whenever it can, and128482.
is a valid number literal, even without anything after the decimal point.
– cHao
Mar 3 '12 at 2:14
1
* Possible loss of accuracy for numbers greater than 2^53
– c01nd01r
Feb 13 '18 at 9:14
This is good, But if I want to remove look-alike characters (1-l or 0-O) what can I do? and for working with long numbers?
– QMaster
May 23 '18 at 20:27
add a comment |
2
Free DEMO: jsfiddle.net/ewsmJ
– paislee
Mar 3 '12 at 2:06
7
As for why128482.toString(36)
doesn't work, but128482..toString(36)
(note the two dots) does...JS likes to snarf the first dot for the number whenever it can, and128482.
is a valid number literal, even without anything after the decimal point.
– cHao
Mar 3 '12 at 2:14
1
* Possible loss of accuracy for numbers greater than 2^53
– c01nd01r
Feb 13 '18 at 9:14
This is good, But if I want to remove look-alike characters (1-l or 0-O) what can I do? and for working with long numbers?
– QMaster
May 23 '18 at 20:27
2
2
Free DEMO: jsfiddle.net/ewsmJ
– paislee
Mar 3 '12 at 2:06
Free DEMO: jsfiddle.net/ewsmJ
– paislee
Mar 3 '12 at 2:06
7
7
As for why
128482.toString(36)
doesn't work, but 128482..toString(36)
(note the two dots) does...JS likes to snarf the first dot for the number whenever it can, and 128482.
is a valid number literal, even without anything after the decimal point.– cHao
Mar 3 '12 at 2:14
As for why
128482.toString(36)
doesn't work, but 128482..toString(36)
(note the two dots) does...JS likes to snarf the first dot for the number whenever it can, and 128482.
is a valid number literal, even without anything after the decimal point.– cHao
Mar 3 '12 at 2:14
1
1
* Possible loss of accuracy for numbers greater than 2^53
– c01nd01r
Feb 13 '18 at 9:14
* Possible loss of accuracy for numbers greater than 2^53
– c01nd01r
Feb 13 '18 at 9:14
This is good, But if I want to remove look-alike characters (1-l or 0-O) what can I do? and for working with long numbers?
– QMaster
May 23 '18 at 20:27
This is good, But if I want to remove look-alike characters (1-l or 0-O) what can I do? and for working with long numbers?
– QMaster
May 23 '18 at 20:27
add a comment |
For anyone looking for how to encode a string in base36 (since this question, How do i convert string to base36 in javascript , is redirected here) -
Here's what I came up with.
/* 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
*/
What kind of strings are accepted? A string can be anything.
– Maarten Bodewes
Jan 27 at 16:12
Also: this seems to always use 2 characters per character. This is great, but it doesn't provide any length advantages; you might as well use hexadecimals.
– Maarten Bodewes
Jan 27 at 16:23
add a comment |
For anyone looking for how to encode a string in base36 (since this question, How do i convert string to base36 in javascript , is redirected here) -
Here's what I came up with.
/* 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
*/
What kind of strings are accepted? A string can be anything.
– Maarten Bodewes
Jan 27 at 16:12
Also: this seems to always use 2 characters per character. This is great, but it doesn't provide any length advantages; you might as well use hexadecimals.
– Maarten Bodewes
Jan 27 at 16:23
add a comment |
For anyone looking for how to encode a string in base36 (since this question, How do i convert string to base36 in javascript , is redirected here) -
Here's what I came up with.
/* 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
*/
For anyone looking for how to encode a string in base36 (since this question, How do i convert string to base36 in javascript , is redirected here) -
Here's what I came up with.
/* 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
*/
edited May 23 '17 at 11:47
Community♦
11
11
answered Jun 21 '16 at 21:01
imjoshimjosh
3,19311220
3,19311220
What kind of strings are accepted? A string can be anything.
– Maarten Bodewes
Jan 27 at 16:12
Also: this seems to always use 2 characters per character. This is great, but it doesn't provide any length advantages; you might as well use hexadecimals.
– Maarten Bodewes
Jan 27 at 16:23
add a comment |
What kind of strings are accepted? A string can be anything.
– Maarten Bodewes
Jan 27 at 16:12
Also: this seems to always use 2 characters per character. This is great, but it doesn't provide any length advantages; you might as well use hexadecimals.
– Maarten Bodewes
Jan 27 at 16:23
What kind of strings are accepted? A string can be anything.
– Maarten Bodewes
Jan 27 at 16:12
What kind of strings are accepted? A string can be anything.
– Maarten Bodewes
Jan 27 at 16:12
Also: this seems to always use 2 characters per character. This is great, but it doesn't provide any length advantages; you might as well use hexadecimals.
– Maarten Bodewes
Jan 27 at 16:23
Also: this seems to always use 2 characters per character. This is great, but it doesn't provide any length advantages; you might as well use hexadecimals.
– Maarten Bodewes
Jan 27 at 16:23
add a comment |
For anyone looking to decode @imjosh's answer in python (say if you've encoded client-side and need to decode server-side), this is what I used. I would have left as a comment in @imjosh's answer but comments don't format very well.
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
and a not-as-elegant Objective-C version:
+ (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;
}
Please specify what the function does and what kind of inputs are expected. As it is, this is a code only answer.
– Maarten Bodewes
Jan 27 at 16:13
add a comment |
For anyone looking to decode @imjosh's answer in python (say if you've encoded client-side and need to decode server-side), this is what I used. I would have left as a comment in @imjosh's answer but comments don't format very well.
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
and a not-as-elegant Objective-C version:
+ (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;
}
Please specify what the function does and what kind of inputs are expected. As it is, this is a code only answer.
– Maarten Bodewes
Jan 27 at 16:13
add a comment |
For anyone looking to decode @imjosh's answer in python (say if you've encoded client-side and need to decode server-side), this is what I used. I would have left as a comment in @imjosh's answer but comments don't format very well.
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
and a not-as-elegant Objective-C version:
+ (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;
}
For anyone looking to decode @imjosh's answer in python (say if you've encoded client-side and need to decode server-side), this is what I used. I would have left as a comment in @imjosh's answer but comments don't format very well.
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
and a not-as-elegant Objective-C version:
+ (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;
}
edited Nov 14 '18 at 17:36
answered Jun 30 '18 at 20:31
Eric D'SouzaEric D'Souza
529418
529418
Please specify what the function does and what kind of inputs are expected. As it is, this is a code only answer.
– Maarten Bodewes
Jan 27 at 16:13
add a comment |
Please specify what the function does and what kind of inputs are expected. As it is, this is a code only answer.
– Maarten Bodewes
Jan 27 at 16:13
Please specify what the function does and what kind of inputs are expected. As it is, this is a code only answer.
– Maarten Bodewes
Jan 27 at 16:13
Please specify what the function does and what kind of inputs are expected. As it is, this is a code only answer.
– Maarten Bodewes
Jan 27 at 16:13
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9542726%2fis-it-possible-to-base-36-encode-with-javascript-jquery%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown