Radix 128 integer representation of a string

algorithmshash functionnumber theory

I am reading a textbook "Introduction to Algorithms" (Cormen, 3rd edition). In the text, the string 'pt' is converted to ASCII such that $p=112$ and $t=116$. The result is then converted to a radix-128 integer by using the following conversion $112\times128 + 116 = 14452 $.

It looks like the conversion proceeds as follows; multiply the first ASCII equivalent ($p=112$ in this case) by the base, $128$, and then add the additional ASCII equivalents. Can I use this conversion scheme for any string regardless of length? For example, lets say I want to convert the string 'pto' to a radix-128 integer. Since $o=111$ in ASCII, is the corresponding radix-128 integer simply $ 112\times128 + 116 + 111 = 14563 $?

I have done some google searches to find clarification; however, I am not having much luck. I don't think I am asking google the right questions. I would greatly appreciate insight as to why this conversion works (assuming it does). Basically, the notion of what we are doing here is escaping me. I would greatly appreciate a response that teaches me to fish rather than just gives me a fish.

Best Answer

Yes, this is the standard way of expressing a number in any base (radix). In base $10$, we have $123=1\cdot 10^2+2 \cdot 10+3\cdot 10^0$. In base $b$ we have $123_b=1\cdot b^2 +2 \cdot b +3 \cdot b^0$. You use as many digits as necessary, increasing the power of the base by $1$ for each digit. You need the base to be (at least) the number of different symbols you are using if you want to represent a string unambiguously.

Related Question