Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Convert numbers to "non-ascii" strings

Reply
Thread Tools

Convert numbers to "non-ascii" strings

 
 
Dave M
Guest
Posts: n/a
 
      02-26-2008
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
def to_ls
i = self
s = ""
4.times { s.concat(i & 0xff); i >>= 8}
return s.reverse
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
 
Reply With Quote
 
 
 
 
Consultant1guru@hotmail.com
Guest
Posts: n/a
 
      02-27-2008
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
> def to_ls
> i = self
> s = ""
> 4.times { s.concat(i & 0xff); i >>= 8}
> return s.reverse
> 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
 
Reply With Quote
 
 
 
 
Clifford Heath
Guest
Posts: n/a
 
      02-27-2008
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
def to_ls
[self].pack("N")
end
end

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

Clifford Heath.
 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      02-27-2008
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
>> def to_ls
>> i = self
>> s = ""
>> 4.times { s.concat(i & 0xff); i >>= 8}
>> return s.reverse
>> 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.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

 
Reply With Quote
 
Dave M
Guest
Posts: n/a
 
      02-28-2008
Joel VanderWerf wrote:
> 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
>>> def to_ls
>>> i = self
>>> s = ""
>>> 4.times { s.concat(i & 0xff); i >>= 8}
>>> return s.reverse
>>> 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.
>


Thanks for the response to both Cliff and Joel. As I suspected, there
was a simple answer right in front of my nose -- I just didn't see it.
Your help is much appreciated.

As to "G", all I have to say is umm ... Thanks for nothing.

Dave M
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert to binary and convert back to strings Harlin Seritt Python 29 02-24-2007 09:33 AM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Numbers to strings to numbers again one man army Javascript 6 12-30-2005 07:05 AM
Frame Numbers vs. JPEG Numbers With CF Cards mort Digital Photography 3 02-16-2005 01:43 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57