Basic python conversion with given string

In PHP, given a string value (integers separated by characters), we can calculate its integer representation:

$hashable = "123A123"; // notice "A" delim
$hash_int = base_convert($hashable, 11, 10);
echo $hash_int;

Output

2151042

This is useful because the result is unique to a huge range of lines (of course, short). I use it to generate an identifier in my application.

How can we do the same transforms in python? Is it possible to generate equal integers for the same lines in PHP and python?

Perhaps first we need to take the hash int of the string hashable, and then convert the integer base, but how exactly do we do it?

+3
source share
1 answer

, 2 36 0 php , php , , , int, , :

def to_base(n, bse):
    digs = "0123456789abcdefghijklmnopqrstuvwxyz"
    tmp = []
    while n:
        n, i = divmod(n, bse)
        tmp.append(digs[i])
    return "".join(tmp[::-1])



def chng_frm_base(s, frm_bse, to_bse):
    if to_bse < 2 or to_bse > 36 or frm_bse < 2 or frm_bse > 36:
        raise ValueError("bases must be between 2-36")
    try:
        return to_base(int(s, frm_bse), to_bse)
    except ValueError:
        try:
            n = int("".join([ch for ch in s if ch.isdigit()]),frm_bse)
            return to_base(n, to_bse)
        except ValueError:
            return 0

:

In [13]: chng_frm_base("123A123", 11, 10)
Out[13]: '2151042'

In [14]: chng_frm_base("123A123", 11, 8)
Out[14]: '10151202'

In [15]: chng_frm_base("123A123", 11, 2)
Out[15]: '1000001101001010000010'

In [16]: chng_frm_base("123A123", 11, 35)
Out[16]: '1f5xc'

In [17]: chng_frm_base("123A123", 11, 1)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-17-9776e0abca26> in <module>()
----> 1 chng_frm_base("123A123", 11, 1)

<ipython-input-2-9c00d800545d> in chng_frm_base(s, frm_bse, to_bse)
     10 def chng_frm_base(s, frm_bse, to_bse):
     11     if to_bse < 2 or to_bse > 36 or frm_bse < 2 or frm_bse > 36:
---> 12         raise ValueError("bases must be between 2-36")
     13     try:
     14         return (to_base(int(s, frm_bse), to_bse))

ValueError: bases must be between 2-36

In [18]: chng_frm_base("hello world!", 10, 2)
Out[18]: 0

, , php-, .

+3

Source: https://habr.com/ru/post/1691650/


All Articles