Base256 Encoding Android
How would you convert an integer to base 62 (like hexadecimal, but with these digits: ‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’).
- Base256 Encoding Android Video
- Base256 Encoding Android App
- Base256 Encoding Android Text
- Base256 Encoding Android Download
I have been trying to find a good Python library for it, but they all seems to be occupied with converting strings. The Python base64 module only accepts strings and turns a single digit into four characters. I was looking for something akin to what URL shorteners use.
Category: Properties: Value: Comments: Basic: Property: data Type: String Default: ' Barcode value to encode Data Matrix Valid Data Char Set: ASCII values 0 - 127 in accordance with the US national version of ISO/IEC 646 NOTE: This version consists of the G0 set of ISO/IEC 646 and the C0 set of ISO/IEC 6429 with values 28 - 31 modified to FS, GS, RS and US respectively. This document provides a complete C# source code for encoding Data Matrix barcode images in C# class using C# Barcode generation.net SDK. OnBarcode C# Barcode Generator is designed to generate, create Data Matrix and other linear & 2d barcodes in Microsoft Word.
There is no standard module for this, but I have written my own functions to achieve that.
Notice the fact that you can give it any alphabet to use for encoding and decoding. If you leave the alphabet
argument out, you are going to get the 62 character alphabet defined on the first line of code, and hence encoding/decoding to/from 62 base.
Hope this helps.
PS – For URL shorteners, I have found that it’s better to leave out a few confusing characters like 0Ol1oI etc. Thus I use this alphabet for my URL shortening needs – '23456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'
Base256 Encoding Android Video
Have fun.
I once wrote a script to do this aswell, I think it’s quite elegant 🙂
Example usage:
The following decoder-maker works with any reasonable base, has a much tidier loop, and gives an explicit error message when it meets an invalid character.
If you’re looking for the highest efficiency (like django), you’ll want something like the following. This code is a combination of efficient methods from Baishampayan Ghose and WoLpH and John Machin.
You may want to also calculate your dictionary in advance. (Note: Encoding with a string shows more efficiency than with a list, even with very long numbers.)
Encoded and decoded 1 million numbers in under 2.5 seconds. (2.2Ghz i7-2670QM)
You probably want base64, not base62. There’s an URL-compatible version of it floating around, so the extra two filler characters shouldn’t be a problem.
The process is fairly simple; consider that base64 represents 6 bits and a regular byte represents 8. Assign a value from 000000 to 111111 to each of the 64 characters chosen, and put the 4 values together to match a set of 3 base256 bytes. Repeat for each set of 3 bytes, padding at the end with your choice of padding character (0 is generally useful).
I have a Python library for doing exactly that here: http://www.djangosnippets.org/snippets/1431/
If all you need is to generate a short ID (since you mention URL shorteners) rather than encode/decode something, this module might help:
I have benefited greatly from others’ posts here. I needed the python code originally for a Django project, but since then I have turned to node.js, so here’s a javascript version of the code (the encoding part) that Baishampayan Ghose provided.
I hope the following snippet could help.
Usage for your case:
Obviously, you can specify another alphabet, consisting of lesser or greater number of symbols, then it will convert your number to the lesser or greater number base. For example, providing ’01’ as an alphabet will output string representing input number as binary.
You may shuffle the alphabet initially to have your unique representation of the numbers. It can be helpful if you’re making URL shortener service.
Personally I like the solution from Baishampayan, mostly because of stripping the confusing characters.
For completeness, and solution with better performance, this post shows a way to use the Python base64 module.
I wrote this a while back and it’s worked pretty well (negatives and all included)
sorry about the length of it all
Here is an recurive and iterative way to do that. The iterative one is a little faster depending on the count of execution.
There is now a python library for this.
I’m working on making a pip package for this.
I recommend you use my bases.py https://github.com/kamijoutouma/bases.py which was inspired by bases.js
refer to https://github.com/kamijoutouma/bases.py#known-basesalphabets
for what bases are usable
Here’s my solution:
Base256 Encoding Android App
explanation
In any base every number is equal to a1+a2*base**2+a3*base**3...
So the goal is to find all the a
s.
For every N=1,2,3...
the code isolates the aN*base**N
by “moduloing” by b
for b=base**(N+1)
which slices all a
s bigger than N
, and slicing all the a
s so that their serial is smaller than N
by decreasing a
everytime the function is called recursively by the current aN*base**N
.
Base%(base-1)1
therefore base**p%(base-1)1
and therefore q*base^p%(base-1)q
with only one exception, when qbase-1
which returns 0
. To fix that case it returns 0
. The function checks for 0
from the beginning.
advantages
In this sample there’s only one multiplication (instead of a division) and some modulus operations, which are all relatively fast.
Sorry, I can’t help you with a library here. I would prefer using base64 and just adding to extra characters to your choice — if possible!
Then you can use the base64 module.
Base256 Encoding Android Text
If this is really, really not possible:
Base256 Encoding Android Download
You can do it yourself this way (this is pseudo-code):