Skip to main content

rdx

A radix conversion module.

Implementation of

Installation

pip install rdx

Usage

This module provides the following functions:

  • int_to_digits(n, b=10): Returns the digits of number n in base b.
  • digits_to_int(d, b=10): Returns the number represented by the digits d in base b.
    >>> from rdx import *
    >>> int_to_digits(42, 2)
    [0, 1, 0, 1, 0, 1]
    >>> int_to_digits(42, 16)
    [10, 2]
    >>> digits_to_int([27, 1], 42)
    69
    
  • int_to_len_digits(n, b=10): Returns the amount of digits the number n represented in base b needs.
    >>> int_to_len_digits(1024, 2)
    11
    
  • digits_to_characters(d, alphabet=digits+ascii_letters): Converts integer digits d to characters of the given alphabet.
  • characters_to_digits(s, alphabet=digits+ascii_letters): Converts characters s of the given alphabet to integer digits.
    >>> digits_to_characters([0, 1, 9, 10, 15, 16])
    ['0', '1', '9', 'a', 'f', 'g']
    >>> characters_to_digits(['0', '1', '9', 'a', 'f', 'g'])
    [0, 1, 9, 10, 15, 16]
    
  • digits_to_string(d, alphabet=string.digits+string.ascii_letters): Converts integer digits d to a right-to-left string in the given alphabet.
  • string_to_digits(s, alphabet=string.digits+string.ascii_letters): Converts a right-to-left string s in the given alphabet to integer digits.
    >>> digits_to_string([0, 0, 15])
    'f00'
    >>> string_to_digits('f12')
    [2, 1, 15]
    

Conventions

Data type

Currently digits or characters are returned as lists. In the future this will be switched to tuples as they are more similar to strings, and a digit representation is similar to a string.

Digit or character arguments should be provided in form of an iterable, not an iterator, as in most functions the will be iterated twice and an iterator will be exhausted after the first run.

Ordering

As a digit representation a list of integers is used, where every element represents a digit, all ordered in ascending positions. So the lowest digit at position with index 0, will be at position 0. But this also means, that when a list of digits is printed, it will be ordered left-to-right, opposed to the usual human-readable right-to-left. E.g. 12 (twelve) corresponds to [2, 1].

int_to_len_digits(n) vs len(int_to_digits(n))

len(int_to_digits(n)) is the naive and non-optimal approach. int_to_len_digits(n) calculates the number of digits intelligently as int(log(n, b))+1 as mentioned in the discussion. But to avoid errors due to math.log using floating point arithmetic sympy.log is used, adding a hefty overhead. A comparison for time critical applications: png (integers with just 3s were choosen because random values introduce to much noise and because 100... seemed to risky for a systematic error and 33... lies exactly between those 100...s on a logarithmic scale)

TODO

License (MIT)

Copyright (c) 2023 Sebastian Gössl

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Release files for rdx 1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rdx 1.1
File Size Uploaded
rdx-1.1.tar.gz 4.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rdx 1.1
File Interpreter ABI Platform
rdx-1.1-py3-none-any.whl Python 3 none any Details

Total release size: 10.0 kB

Release files / rdx-1.1.tar.gz

Download URL rdx-1.1.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
170c099756969ff9a1603569e29d7017bfbb6f9e0164122e1aa9d3079ea0d069
BLAKE2b-256 checksum
How to use checksums
d09fad0cdb56e7bd6e60c0b8287351da67bc36e5deca1d3336f6134115ad38e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release files / rdx-1.1-py3-none-any.whl

Download URL rdx-1.1-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f6885d6109cba62edecf53fbd94e7aac62085fadc7dbebe6bc41424f72dbc0d3
BLAKE2b-256 checksum
How to use checksums
0a7fc4269d3c8a4297bcfa6aac76e393004492c8b00688f98c73dfdbdd83bb8e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release history Release notifications | RSS feed

This release

1.1 This release

2 release files

1.0

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.9

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page