![]() Operations can be done: the Number class returns an instance of basencode.Integer if the provided number is an Integer, else it returns a basencode.Float > n3 = Number(54321) # the Number class returns an instance of `basencode.Integer` if the provided number is an Integer, otherwise it returns a `basencode.Float`. > n2.repr_in_base(577, mode="l") # the program remembers the digits for base 577 now The mode parameter determines how the value of the representation will determine how (list or string) the answer will be returned. How would you add any bases you want? Let me replicate the example of the currently most upvoted answer: the digits parameter allows you to override the default digits from base 2 to 64, and provide digits for any base higher than that. > n1.repr_in_base(33) # yet another base - all bases from 2 to 64 are supported from the start > n1.repr_in_base(2, digits=list('-+')) # override default digits: use `-` and `+` in place of `0` and `1` > n1.repr_in_bin() # equivelant to `n1.repr_in_base(2)` > Number('30V', 64) # construct Integer from base 64 > n1.repr_in_base(64) # convert to base 64 Here's a possible use case: > from basencode import * While the currently top answer is definitely an awesome solution, there remains more customization users might like.īasencode adds some of these features, including conversions of floating point numbers, modifying digits (in the linked answer, only numbers can be used). """convert positive decimal integer n to equivalent in another base (2-36)"""ĭigits = "0123456789abcdefghijklmnopqrstuvwxyz" Here's another one from the same link def baseconvert(n, base): New_num_string=remainder_string+new_num_string Up to base-36 is supported without special notation.""" If you need compatibility with ancient versions of Python, you can either use gmpy (which does include a fast, completely general int-to-string conversion function, and can be built for such ancient versions – you may need to try older releases since the recent ones have not been tested for venerable Python and GMP releases, only somewhat recent ones), or, for less speed but more convenience, use Python code – e.g., for Python 2, most simply: import stringĭigs = string.digits + string.ascii_lettersįor Python 3, int(x / base) leads to incorrect results, and must be changed to x // base: import string Once you are done, it will be obvious why my function returns a list and has the signature it has. I encourage you to think for a little bit why base in your method works only for n this is due to lack of understanding what a number in some base means.If you disagree, post you own solution with a built-in function which can convert a base 10 number to base 577. at some point of time you will notice that sometimes there is no built-in library function to do things that you want, so you need to write your own.Which you can later convert to any base you want NumberToBase(67854 ** 15 - 102, 577), will give you a correct solution: So if you need to convert some super huge number to the base 577, So here is a super simple solution: def numberToBase(n, b): There was no attempt to give a solution which converts to any arbitrary base from 2 to infinity. Surprisingly, people were giving only solutions that convert to small bases (smaller than the length of the English alphabet). How to convert an integer to the shortest url-safe string in Python?.Integer to base-x system using recursion in python.Python elegant inverse function of int(string, base).I want to allow bases other than 2, 8, 16 I want a general solution that can be called the same way for different bases Those functions are not available on older versions of Python, with which I need compatibility with (2.2) I know about the functions bin, oct, hex, but I cannot use them for a few reasons: However, I feel like I must be missing something. ![]() This is an easy function to write: in fact it's easier than describing it in this question. The function name/argument order is unimportant.įor any number x and base b that int() will accept. I want some function int2base(num, base), such that: int(int2base(x, b), b) = x I want to perform the inverse: creation of a string from an integer, Python allows easy creation of an integer from a string of a given base via int(str, base).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |