# Convert numbers to "non-ascii" strings

**URL:** <https://rubytalk.org/t/convert-numbers-to-non-ascii-strings/44797>\
**Category:** ruby-talk\
**Created:** [27 February 2008 06:19 UTC](https://rubytalk.org/t/convert-numbers-to-non-ascii-strings/44797 "2008-02-27T06:19:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Consultant1guru](https://avatars.discourse-cdn.com/v4/letter/c/439d5e/32.png) [@Consultant1guru](https://rubytalk.org/u/Consultant1guru)\
**Post date:** [27 February 2008 06:19 UTC](https://rubytalk.org/t/convert-numbers-to-non-ascii-strings/44797/1 "2008-02-27T06:19:58Z")

</div>

Dave --

I'm afraid that you may have selected the incorrect language for the  
task at hand. Your question implies that you are doing some complex  
parsing or homogenization, and Ruby, while a nice scripting language  
for small tasks, is simply not a good choice for that application. If  
you want to do simple web development or a little scraping, Ruby is  
good for that, but that's about the extent of what it can really  
handle well.

I suggest you look at Perl or TCL for a serious scripting language,  
Java for a robust programming language, and Visual Basic if you want  
an ultra-fast development environment. These are tools that can  
handle the task you describe in a sensible way.

I am not saying that you should give up Ruby. Ruby is great for  
learning some of the object-oriented concepts that while interesting,  
may not be practical for some tasks in the real world.

Hope that helps

Cheers

G

> **···**
>
> On Feb 26, 8:46 am, Dave M \<dmil...@tecolote.net\> wrote:
> 
> > Hello all
> > 
> > I would like to convert integers to strings in a way that I haven't come  
> > across in the Ruby functions I have looked at. I would like the string  
> > to be exactly what the integer would be if I read it in from a binary file.
> > 
> > In other words, the integer 0x61626364 would be the string "abcd" after  
> > the conversion. I can write code to do it, but I think there is  
> > probably a better way than the approach I have come up with.
> > 
> > This works but seems very brute force to me:
> > 
> > # assumes 32 bit numbers.  
> > class Bignum  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def to\_ls  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = self  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s = ""  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4.times { s.concat(i & 0xff); i \>\>= 8}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return s.reverse  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > puts 0x61626364.to\_ls  
> > abcd
> > 
> > Also, it seems that I need this for both Bignum and Fixnum to cover the  
> > entire 32 bit number range.
> > 
> > Thanks
> > 
> > Dave M

---

<div class="post-metadata">

**Author:** ![Clifford\_Heath5](https://avatars.discourse-cdn.com/v4/letter/c/7ab992/32.png) [@Clifford\_Heath5](https://rubytalk.org/u/Clifford_Heath5)\
**Post date:** [27 February 2008 06:34 UTC](https://rubytalk.org/t/convert-numbers-to-non-ascii-strings/44797/2 "2008-02-27T06:34:55Z")

</div>

Consultant1guru@hotmail.com wrote:

> I'm afraid that you may have selected the incorrect language for the  
> task at hand. Your question implies that you are doing some complex  
> parsing or ...

Nothing whatever complex about it, quite simple in Ruby:

class Numeric  
&nbsp;&nbsp;def to\_ls  
&nbsp;&nbsp;&nbsp;&nbsp;[self].pack("N")  
&nbsp;&nbsp;end  
end

Works for BigNum and FixNum, no need to modify those classes.

Clifford Heath.

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [27 February 2008 06:39 UTC](https://rubytalk.org/t/convert-numbers-to-non-ascii-strings/44797/3 "2008-02-27T06:39:46Z")

</div>

Consultant1guru@hotmail.com wrote:

> **···**
>
> > On Feb 26, 8:46 am, Dave M \<dmil...@tecolote.net\> wrote:
> > 
> > > Hello all
> > > 
> > > I would like to convert integers to strings in a way that I haven't come  
> > > across in the Ruby functions I have looked at. I would like the string  
> > > to be exactly what the integer would be if I read it in from a binary file.
> > > 
> > > In other words, the integer 0x61626364 would be the string "abcd" after  
> > > the conversion. I can write code to do it, but I think there is  
> > > probably a better way than the approach I have come up with.
> > > 
> > > This works but seems very brute force to me:
> > > 
> > > # assumes 32 bit numbers.  
> > > class Bignum  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def to\_ls  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = self  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s = ""  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4.times { s.concat(i & 0xff); i \>\>= 8}  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return s.reverse  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > > end
> > > 
> > > puts 0x61626364.to\_ls  
> > > abcd
> > > 
> > > Also, it seems that I need this for both Bignum and Fixnum to cover the  
> > > entire 32 bit number range.
> > > 
> > > Thanks
> > > 
> > > Dave M
> > 
> > Dave --
> > 
> > I'm afraid that you may have selected the incorrect language for the  
> > task at hand. Your question implies that you are doing some complex  
> > parsing or homogenization, and Ruby, while a nice scripting language  
> > for small tasks, is simply not a good choice for that application. If  
> > you want to do simple web development or a little scraping, Ruby is  
> > good for that, but that's about the extent of what it can really  
> > handle well.
> > 
> > I suggest you look at Perl or TCL for a serious scripting language,  
> > Java for a robust programming language, and Visual Basic if you want  
> > an ultra-fast development environment. These are tools that can  
> > handle the task you describe in a sensible way.
> > 
> > I am not saying that you should give up Ruby. Ruby is great for  
> > learning some of the object-oriented concepts that while interesting,  
> > may not be practical for some tasks in the real world.
> > 
> > Hope that helps
> > 
> > Cheers
> > 
> > G
> 
> Please reconsider your assessment of ruby, G.
> 
> [0x61626364].pack("N")
> 
> This returns the string "abcd" and does so elegantly and efficiently.
> 
> In my experience and that of many others on this mailing list, ruby can be used for a variety of complex tasks, and not just web apps. Also, like perl, it makes simple tasks simple.
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
