Rangecoding with restricted output alphabet

Sometimes we can't use the full range of 0-255 byte values (or unicode symbols),

because of some protocol or storage restrictions.

In this case, people tend to use "obvious" solutions like base64 or even hex-coding

(aka quoted-printable).

But actually arithmetic coding and especially rangecoding provide a universal

method which would work with any alphabet or, in general, output syntax/structure.

Rangecoding comes to mind here because its a version of arithmetic coding

which arbitrarily works in base-256 numeral system.

But there's no (mathematical) reason to restrict oneself with power-of-2

bases there, as demonstrated in this implementation:

http://compression.ru/sh/marcdemo.rar

However, there's even a better way to do this (faster and simpler and more general).

What are these "output restrictions"? In other words, its basically a model.

For example, if only symbols 'a'-'z' are allowed, it means that symbols 'a'-'z'

each have a probability of 1/26, and other symbols' probabilities are 0.

And if we'd try _decoding_ any input using a simple rangecoder and the described

model, there'd be obviously only 'a'-'z' on output. And after encoding that

output back with the same model, we'd be able to restore the original input,

as demonstrated here:

http://nishi.dreamhosters.com/u/marc_v1.rar

Still, if we'd try to _compress_ the data with output restrictions, this

second method would require to first encode the data using "input model",

then decode with "output model", while first method supports both

input models and output alphabet restrictions at once

Applications of this for steganography/watermarking are much more interesting

than simple coding with restricted output though.