# String conversion, from a base to an other new one

**URL:** <https://www.ruby-forum.com/t/string-conversion-from-a-base-to-an-other-new-one/171596>\
**Category:** Ruby\
**Created:** [July 6, 2009, 1:46pm UTC](https://www.ruby-forum.com/t/string-conversion-from-a-base-to-an-other-new-one/171596 "2009-07-06T13:46:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [July 6, 2009, 1:46pm UTC](https://www.ruby-forum.com/t/string-conversion-from-a-base-to-an-other-new-one/171596/1 "2009-07-06T13:46:16Z")

</div>

Hello,

Is there a way, in Ruby, to convert this kind of value: “3m74jnfu9f”  
(which is in base 32 having: “0123456789abcdefghijklmnopqrstuv” chars)  
into a other new base, for instance a base 8 (having: “Ã¹=\*$`^~Â£” chars)?

.to\_s method seems intersting, its allow to pass into many bases such  
with str.to\_s(8), str.to\_s(16), str.to\_s(32)… but I don’t see how to  
change chars.

Thank you for any suggestions.

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [July 6, 2009, 3:17pm UTC](https://www.ruby-forum.com/t/string-conversion-from-a-base-to-an-other-new-one/171596/2 "2009-07-06T15:17:57Z")

</div>

On 7/6/09, Zangief I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,
> 
> Is there a way, in Ruby, to convert this kind of value: “3m74jnfu9f”  
> (which is in base 32 having: “0123456789abcdefghijklmnopqrstuv” chars)  
> into a other new base, for instance a base 8 (having: “ù=\*$`^~£” chars)?  
> I have used abcdefgh for readability but I guess you got the idea 😉

irb(main):006:0\> “3m74jnfu9f”.to\_i(32).to\_s(8).tr(“01234567”,“abcdefgh”)  
=\> “dfedeeehdfhheefh”

HTH  
Robert

–  
Toutes les grandes personnes ont d’abord été des enfants, mais peu  
d’entre elles s’en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exupéry]

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [July 6, 2009, 7:08pm UTC](https://www.ruby-forum.com/t/string-conversion-from-a-base-to-an-other-new-one/171596/3 "2009-07-06T19:08:28Z")

</div>

Work perfecly. Thanks!
