I've been looking online, but can't find a solution to this. In Python, Ruby, or Java, how can I base 36 encode the following string: nOrG9Eh0uyeilM8Nnu5pTywj3935kW+5=
Asked
Active
Viewed 6,407 times
5
-
Encode or decode? It looks encoded. – Kaj May 18 '11 at 18:57
-
2This has **nothing** to do with cryptography. – SLaks May 18 '11 at 18:57
-
I want to encode that string and then decode it back in Base36. I just want to know if it's even possible. – Bradford May 18 '11 at 18:57
-
To all the people sharing code to encode a number, thanks, but I'm trying to encode a string. Please excuse me if that's not possible. I see libraries that can base 64 encode a string, though. – Bradford May 18 '11 at 19:00
-
@SLaks Encryption also includes algorithms that are hopelessly naive ;) – gshauger May 18 '11 at 19:10
4 Answers
9
Ruby
To base 36:
s.unpack('H*')[0].to_i(16).to_s 36
From base 36:
[s36.to_i(36).to_s(16)].pack 'H*'

DigitalRoss
- 143,651
- 25
- 248
- 329
1
I have just done this:
static String encodeRootId(final String value) {
try {
final BigInteger bigInteger = new BigInteger(value.getBytes("UTF-8"));
final String encoded = bigInteger.toString(Character.MAX_RADIX);
//must encode the sign as well
if (bigInteger.signum() < 0) {
return 'n' + encoded.substring(1);
} else {
return 'p' + encoded;
}
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("impossible");
}
}
the trick to convert the string bytes[] to a big int brought the disadvantage of having to manually encode the possible -, admittedly not pretty but a quick solution.
also, in my use case i dont have to decode and performance is not a concern.

elonderin
- 509
- 4
- 12
0
Looks like wikipedia has an article on how to do it in python: http://en.wikipedia.org/wiki/Base_36

Kaj
- 10,862
- 2
- 33
- 27
-1
This could be seen as a duplicate as another question... Python base 36 encoding.
Basically, there is this example on wikipedia:
def base36encode(number, alphabet='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'):
"""Convert positive integer to a base36 string."""
if not isinstance(number, (int, long)):
raise TypeError('number must be an integer')
# Special case for zero
if number == 0:
return alphabet[0]
base36 = ''
sign = ''
if number < 0:
sign = '-'
number = - number
while number != 0:
number, i = divmod(number, len(alphabet))
base36 = alphabet[i] + base36
return sign + base36
def base36decode(number):
return int(number, 36)
print base36encode(1412823931503067241)
print base36decode('AQF8AA0006EH')
Wikipedia page: http://en.wikipedia.org/wiki/Base_36

Community
- 1
- 1

Joseph Redfern
- 939
- 1
- 6
- 13
-
2This answers the question of how to base 36 encode a numeric value, but not a string. – Bradford May 18 '11 at 19:09
-
-
1@Bradford: Just convert your string to a number and encode it. Reverse the steps for decoding. For example if you're using 8-bit characters, consider each character code to be a Base 256 digit. – martineau May 18 '11 at 19:55
-
1@Bradford: Here's a quick way to convert a string of 8-bit characters into a number: `reduce(lambda a,d: a*256 + ord(d), astring, 0)`. – martineau May 18 '11 at 20:02
-
Wow. Thanks a lot martineau. That's actually the answer I was looking for. It was easy to reverse with divmod too. – Bradford May 19 '11 at 01:55