Turning a string into array of ASCII bytes

What is the shortest, most straightforward way (without temporary
variables, etc)?

My best route is now “1234”.split(//).collect{|c|c[0]} but I’m sure
there is a much better way. Also it’s a bit slow.

···


dave

What is the shortest, most straightforward way (without temporary
variables, etc)?

My best route is now “1234”.split(//).collect{|c|c[0]} but I’m sure
there is a much better way. Also it’s a bit slow.

irb(main):003:0> “1234”.unpack(“c*”)
=> [49, 50, 51, 52]

Is that what you want?

Peter

Do you really need an array? You can access the ASCII codes of every
character in a string with .

irb(main):001:0> a=‘David’
=> “David”
irb(main):002:0> a[0]
=> 68
irb(main):003:0> a[1]
=> 97

···

On 2003-11-26 14:39:45, David Garamond wrote:

What is the shortest, most straightforward way (without temporary
variables, etc)?

Peter wrote:

What is the shortest, most straightforward way (without temporary
variables, etc)?

My best route is now “1234”.split(//).collect{|c|c[0]} but I’m sure
there is a much better way. Also it’s a bit slow.

irb(main):003:0> “1234”.unpack(“c*”)
=> [49, 50, 51, 52]

Is that what you want?

Of course. I forgot all about pack/unpack.

Thanks!

···


dave

What is the shortest, most straightforward way (without temporary
variables, etc)?

My best route is now “1234”.split(//).collect{|c|c[0]} but I’m sure
there is a much better way. Also it’s a bit slow.

irb(main):003:0> “1234”.unpack(“c*”)
=> [49, 50, 51, 52]

Another possible solution could be (if you use Ruby-1.8.1):

ruby a.rb
[“78”, “79”, “7a”]
cat a.rb
require ‘enumerator’
str = “xyz”
enum = Enumerable::Enumerator.new(str, :each_byte)
p enum.map {|b| ‘%02x’ % b } #=> [“78”, “79”, “7a”]

···

On Wed, 26 Nov 2003 22:44:37 +0900, Peter wrote:


Simon Strandgaard

Currently, that’s so. However, I believe that such behavior is due to
be phased out in the future, in order to have subscripting be more consistent
(i.e. so that s[0] and s[0,1] return the same thing, as is already true
of arrays).

-Mark

···

On Wed, Nov 26, 2003 at 04:14:43PM +0100, Stefan Scholl wrote:

On 2003-11-26 14:39:45, David Garamond wrote:

What is the shortest, most straightforward way (without temporary
variables, etc)?

Do you really need an array? You can access the ASCII codes of every
character in a string with .

I:>irb
irb(main):001:0> Time.now
=> Wed Nov 26 11:29:24 Eastern Standard Time 2003
irb(main):002:0> Time.now.gmtime
=> Wed Nov 26 16:29:29 UTC 2003
irb(main):003:0>

I need to be able to return the zone by it’s numeric representation(
i.e. -0500 ). Like

Wed Nov 26 16:29:29 UTC 2003 -0500

by chance.

I cannot find a function to do this. Is it built in or can i request an
addition?

Thanks,

Zach

“Mark J. Reed” markjreed@mail.com schrieb im Newsbeitrag
news:20031126161656.GA617@mulan.thereeds.org

What is the shortest, most straightforward way (without temporary
variables, etc)?

Do you really need an array? You can access the ASCII codes of every
character in a string with .

Currently, that’s so. However, I believe that such behavior is due to
be phased out in the future, in order to have subscripting be more
consistent
(i.e. so that s[0] and s[0,1] return the same thing, as is already true
of arrays).

But you can be sure that then there will be another method (possibly
String#at(index)) that is equivalent to String#[index] of today.

robert
···

On Wed, Nov 26, 2003 at 04:14:43PM +0100, Stefan Scholl wrote:

On 2003-11-26 14:39:45, David Garamond wrote:

Who wants to use such a language? I don’t want to rewrite my code
every few months.

If these tiny little methods aren’t stable enough to be mentioned in
an answer then I’m really wrong here. :frowning:

···

On 2003-11-26 17:16:56, Mark J. Reed wrote:

On Wed, Nov 26, 2003 at 04:14:43PM +0100, Stefan Scholl wrote:

Do you really need an array? You can access the ASCII codes of every
character in a string with .

Currently, that’s so. However, I believe that such behavior is due to
be phased out in the future, in order to have subscripting be more consistent
(i.e. so that s[0] and s[0,1] return the same thing, as is already true
of arrays).

(You should have deleted the “UTC” from that, since it refers to the
time zone; it’s the modern name for GMT.)

You want the DateTime class, included in the ‘date’ library which comes
with Ruby:

irb(main):001:0> require ‘date’
=> true
irb(main):002:0> n=DateTime.now
=> #<DateTime: 105968312236111499/43200000000,-5/24,2299161>
irb(main):003:0> n.to_s
=> “2003-11-26T11:34:32-0500”
irb(main):004:0> n.zone
=> “-0500”
irb(main):005:0> n.strftime(“%a %b %d %H:%M:%S %Y %Z”)
=> “Wed Nov 26 11:34:32 2003 -0500”

However, as I have posted here before, there is a bug in this
library which for some reason automatically increases the time zone
offset from UTC by one minute, so if you run the above examples on your
system you will probably see -0501 instead of -0500. This problem is
easily fixed by removing the line

d += d / d.abs if d.nonzero?

in the definition of DateTime.now (line 511 of date.rb).

-Mark

···

On Thu, Nov 27, 2003 at 01:31:08AM +0900, Zach Dennis wrote:

I need to be able to return the zone by it’s numeric representation(
i.e. -0500 ). Like

Wed Nov 26 16:29:29 UTC 2003 -0500

or

str[index].asc as the opposite of Integer#chr, perhaps?

i thought we already had this but i can’t seem to find it so guess we don’t.

-t0

···

On Wednesday 26 November 2003 06:07 pm, Robert Klemme wrote:

(i.e. so that s[0] and s[0,1] return the same thing, as is already true
of arrays).

But you can be sure that then there will be another method (possibly
String#at(index)) that is equivalent to String#[index] of today.

You don’t have to. Ruby has evolved relatively slowly, and
incompatible changes such as this one have been few and far
between. This one hasn’t been made yet, you’ll notice, and there
will probably be a long period where the current behavior still
works but generates a warning. I wouldn’t be surprised if the
final change were left out until Ruby 2.0 - and a major revision number
change means you’re allowed to break things. :slight_smile:

-Mark

···

On Wed, Nov 26, 2003 at 07:38:13PM +0100, Stefan Scholl wrote:

Who wants to use such a language? I don’t want to rewrite my code
every few months.

Oh, it turns out that instead of using DateTime, you can just use the %z
specifier to strftime.

irb(main):001:0> n=Time.now
=> Wed Nov 26 11:41:51 EST 2003
irb(main):002:0> n.zone
=> “EST”
irb(main):003:0> n.strftime(“%Z”)
=> “EST”
irb(main):004:0> n.strftime(“%z”)
=> “-0500”
irb(main):005:0> n.strftime(“%a %b %d %H:%M:%S %z %Y”)
=> “Wed Nov 26 11:41:51 -0500 2003”

-Mark

···

On Wed, Nov 26, 2003 at 04:39:45PM +0000, Mark J. Reed wrote:

On Thu, Nov 27, 2003 at 01:31:08AM +0900, Zach Dennis wrote:

I need to be able to return the zone by it’s numeric representation(
i.e. -0500 ). Like

“T. Onoma” transami@runbox.com schrieb im Newsbeitrag
news:200311260931.14413.transami@runbox.com

(i.e. so that s[0] and s[0,1] return the same thing, as is already
true
of arrays).

But you can be sure that then there will be another method (possibly
String#at(index)) that is equivalent to String#[index] of today.

or

str[index].asc as the opposite of Integer#chr, perhaps?

Inefficient since in the future str[index] would return a string (see the
other postings in the thread). It would be a bad idea to create a new
string just to get at the ascii value of it’s first character.

i thought we already had this but i can’t seem to find it so guess we
don’t.

Currently str[index] returns an int denoting the ascii value.

robert
···

On Wednesday 26 November 2003 06:07 pm, Robert Klemme wrote:

you mean this?

~ > irb
irb(main):001:0> 42.chr
=> “*”

irb(main):002:0> 42.chr[0]
=> 42

irb(main):003:0> “*”[0]
=> 42

irb(main):004:0> “"[0].chr
=> "

-a

···

On Thu, 27 Nov 2003, T. Onoma wrote:

Date: Thu, 27 Nov 2003 02:37:02 +0900
From: T. Onoma transami@runbox.com
Newsgroups: comp.lang.ruby
Subject: Re: turning a string into array of ASCII bytes

On Wednesday 26 November 2003 06:07 pm, Robert Klemme wrote:

(i.e. so that s[0] and s[0,1] return the same thing, as is already true
of arrays).

But you can be sure that then there will be another method (possibly
String#at(index)) that is equivalent to String#[index] of today.

or

str[index].asc as the opposite of Integer#chr, perhaps?

i thought we already had this but i can’t seem to find it so guess we don’t.

-t0

ATTN: please update your address books with address below!

===============================================================================

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
STP :: Solar-Terrestrial Physics Data | NCEI
NGDC :: http://www.ngdc.noaa.gov/
NESDIS :: http://www.nesdis.noaa.gov/
NOAA :: http://www.noaa.gov/
US DOC :: http://www.commerce.gov/

The difference between art and science is that science is what we
understand well enough to explain to a computer.
Art is everything else.
– Donald Knuth, “Discover”

/bin/sh -c ‘for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done’
===============================================================================

I’ve just read your mail. I don’t read nearly all discussions in
English now.

I think you are right. I intended to consider leap seconds. But, I
was in confusion.

Thank you for your report.

–tadf

— date.rb 2003-08-12 05:09:41+09 2.9
+++ date.rb 2004-01-17 16:51:52+09
@@ -1,4 +1,4 @@
-# date.rb: Written by Tadayoshi Funaba 1998-2003
+# date.rb: Written by Tadayoshi Funaba 1998-2004

$Id: date.rb,v 2.9 2003-08-12 05:09:41+09 tadf Exp tadf $

require ‘rational’
@@ -508,8 +508,7 @@
jd = civil_to_jd((a[0,3] << sg))
fr = time_to_day_fraction(
(a[3,3])) + i.usec.to_r/86400000000
d = Time.gm(*i.to_a).to_i - i.to_i

  • d += d / d.abs if d.nonzero?
  • of = (d / 60).to_r/1440
  • of = (d / 60.0).round.to_r/1440
    new0(jd_to_ajd(jd, fr, of), of, sg)
    end
···

At 2003-11-27T01:42:12+0900 markjreed@mail.com (Mark J. Reed) wrote:

However, as I have posted here before, there is a bug in this
library which for some reason automatically increases the time zone
offset from UTC by one minute, so if you run the above examples on your
system you will probably see -0501 instead of -0500. This problem is
easily fixed by removing the line

Thank you very much Mark!

Zach

···

-----Original Message-----
From: Mark J. Reed [mailto:markjreed@mail.com]
Sent: Wednesday, November 26, 2003 11:47 AM
To: ruby-talk ML
Subject: Re: Date Class and GMT

On Wed, Nov 26, 2003 at 04:39:45PM +0000, Mark J. Reed wrote:

On Thu, Nov 27, 2003 at 01:31:08AM +0900, Zach Dennis wrote:

I need to be able to return the zone by it’s numeric representation(
i.e. -0500 ). Like

Oh, it turns out that instead of using DateTime, you can just use the %z
specifier to strftime.

irb(main):001:0> n=Time.now
=> Wed Nov 26 11:41:51 EST 2003
irb(main):002:0> n.zone
=> “EST”
irb(main):003:0> n.strftime(“%Z”)
=> “EST”
irb(main):004:0> n.strftime(“%z”)
=> “-0500”
irb(main):005:0> n.strftime(“%a %b %d %H:%M:%S %z %Y”)
=> “Wed Nov 26 11:41:51 -0500 2003”

-Mark