I posted this at RMRK a couple of days ago. This is just something awesome about Ruby I noticed, I can’t really think of any practical uses for this. It’s basically just harnessing the String.to_i(base) function that Ruby comes built-in with. I created a method that makes it simpler, and fixes some bugs with converting to base 1. The way it works is:

String.convert_base(from, to) – where String is an instance of the String class (anything in quotation marks), even letters, where from is the base you are converting from, and to is the base you’d like it to convert to. Of course, this only works up to base 36 (ten digits + 26 letters), and down to base 1, because other bases are kinda impossible without hard-coding them in. And they wouldn’t be practical at all anyway.

Here are some examples: “38”.convert_base(10, 2) -> 100110. This has converted 38 from decimal to binary. “”.convert_base(2, 10) -> 284. This has converted from binary to decimal. “Pacman”.convert_base(36, 10) -> 1529039327. This has (awesomely) converted “Pacman” from base 36 (where all letters of the alphabet have a numerical value) to decimal. “2073739462”.convert_base(10, 36) -> “yanfly”. This has converted those numbers into letters.

Anyway, you can get the snippet here. Play around with it, whatever. Note that it’s incredibly cool, because if you’re a nerd like me you’ll have fun converting numbers to letters and vice-avers with from = 36 and to = 10, or the other way around. That is all. I’m a bit behind on my scripting, sorry about that. Turns out school’s a bitch. Yanfly’s taking a well-deserved scripting break, so I might do some catching up while he does that. I’ve got holidays coming up soon, so I anticipate some scripting then.

Enjoy, I’ll probably be back with a script next time.

### Like this:

Like Loading...

*Related*

OMG PAC SO AWESOME

972750!

1511913, 1361833443 1796184084, 1092449 1166 807242 38210 56092996894 879 1215663674668 32213 54564641 18979156 22420 531887 1436485 20331 38210 1288296867033239.

Do note that the underlying function you use only supports base conversion between 2 and 36 both inclusive. You have not really fixed any radix = 1 bugs, but rather specifically added that functionality.

For consistency I suggest using the character 0 rather than 1. To make the logic more explicit the following symbols are part of the following bases:

(…)

base-10: 0,1,2,3,4,5,6,7,8,9

(…)

base-4: 0,1,2,3

base-3: 0,1,2

base-2: 0,1

base-1: 1

See how strange the last one appears?

Can you on a different note imagine why base-1 was left out?

I did think of this, but remembered that in the practical uses of the unary number system the numbers are usually written in strokes (I guess vertical bars |). 1 looks more like a stroke than 0, so to save confusion I used 1.

I would assume that base 1 was left out because there really aren’t any practical uses (that I can think of, anyway). There’s probably a much more sensible reason, but I can’t be bothered thinking at this hour in the morning (11:55 AM).

I also couldn’t be bothered converting that sentence. Sorry.