Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 819377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:17:55+00:00 2026-05-15T02:17:55+00:00

I am looking for a way to convert a base-10 number into a base-N

  • 0

I am looking for a way to convert a base-10 number into a base-N number where N can be large. Specifically i am looking at converting to base-85 and back again. Does anyone know a simple algorithm to perform the conversion? Ideally it would provide something like:

to_radix(83992, 85) -> [11, 53, 12]

Any ideas are appreciated!

Roja

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:17:56+00:00Added an answer on May 15, 2026 at 2:17 am

    That was kind of an interesting question, so I went a little overboard:

    class Integer
      def to_base(base=10)
        return [0] if zero?
        raise ArgumentError, 'base must be greater than zero' unless base > 0
        num = abs
        return [1] * num if base == 1
        [].tap do |digits|
          while num > 0
            digits.unshift num % base
            num /= base
          end
        end
      end
    end
    

    This works for arbitrary bases. It only works for integers, although there is no reason why it couldn’t be extended to work with any arbitrary number. Also, it ignores the sign of the number. Again, there is no reason why it must do that, but mainly I didn’t want to have to come up with a convention for returning the sign in the return value.

    class Integer
      old_to_s = instance_method(:to_s)
      define_method :to_s do |base=10, mapping=nil, sep=''|
        return old_to_s.bind(self).(base) unless mapping || base > 36
        mapping ||= '0123456789abcdefghijklmnopqrstuvwxyz'
        return to_base(base).map {|digit| mapping[digit].to_s }.join(sep)
      end
    end
    
    [Fixnum, Bignum].each do |klass|
      old_to_s = klass.instance_method(:to_s)
      klass.send :define_method, :to_s do |base=10, mapping=nil, sep=''|
        return old_to_s.bind(self).(base) unless mapping || base > 36
        return super(base, mapping, sep) if mapping
        return super(base)
      end
    end
    

    I also extended the to_s method so that it works with bases greater than 36. If you want to use a base greater than 36, you have to pass in a mapping object which maps the “digits” to strings. (Well, actually, all that is required is that you provide an object that responds to [] and returns something which responds to to_s. So, a string is perfect, but e.g. an array of integers also works.)

    It also accepts an optional separator, which is used to separate the digits.

    For example, this allows you to format an IPv4 address by treating it as a base-256 number and using the identity for the mapping and '.' as the separator:

    2_078_934_278.to_s(256, Array.new(256) {|i| i }, '.') # => '123.234.5.6'
    

    Here’s an (incomplete) testsuite:

    require 'test/unit'
    class TestBaseConversion < Test::Unit::TestCase
      def test_that_83992_in_base_85_is_11_53_12
        assert_equal [11, 53, 12], 83992.to_base(85)
      end
      def test_that_83992_in_base_37_is_1_24_13_2
        assert_equal [1, 24, 13, 2], 83992.to_base(37)
      end
      def test_that_84026_in_base_37_is_1_24_13_36
        assert_equal [1, 24, 13, 36], 84026.to_base(37)
      end
      def test_that_0_in_any_base_is_0
        100.times do |base|
          assert_equal [0], 0.to_base(base)
          assert_equal [0], 0.to_base(1 << base)
          assert_equal [0], 0.to_base(base << base)
        end
      end
      def test_that_84026_in_base_37_prints_1od_
        assert_equal '1od_', 84026.to_s(37, '0123456789abcdefghijklmnopqrstuvwxyz_')
      end
      def test_that_ip_address_formatting_works
        addr = 2_078_934_278
        assert_equal '123.234.5.6', addr.to_s(256, (0..255).to_a, '.')
        assert_equal '123.234.5.6', addr.to_s(256, Array.new(256) {|i| i}, '.')
      end
      def test_that_old_to_s_still_works
        assert_equal '84026', 84026.to_s
        assert_equal '1su2', 84026.to_s(36)
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As the title says i am looking for a way convert a huge number
I'm looking to convert for a way to my classes to XML and back
I'm looking for a way to convert an XML Schema definition file into an
I'm looking for an easy way to convert a simple binary file into a
I am looking for a way to convert any number to a percentage in
I'm looking for a way to convert a BigInteger into a very short String
I'm looking for a way to convert a preprocessor token to a string. Specifically,
I'm looking for av way to convert a string into whitespace; spaces, newlines and
I'm looking for the simplest way to convert a string containing valid XML into
I initially was looking for a way convert byte to float, and found answers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.