turning a string into array of ASCII bytes

D

David Garamond

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.
 
S

Stefan Scholl

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 [].

irb(main):001:0> a='David'
=> "David"
irb(main):002:0> a[0]
=> 68
irb(main):003:0> a[1]
=> 97
 
R

Robert Klemme

Mark J. Reed said:
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
 
T

T. Onoma

(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
 
A

Ara.T.Howard

Date: Thu, 27 Nov 2003 02:37:02 +0900
From: T. Onoma <[email protected]>
Newsgroups: comp.lang.ruby
Subject: Re: turning a string into array of ASCII bytes

(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

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
--

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 :: http://www.ngdc.noaa.gov/stp/
| 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'
===============================================================================
 
S

Stefan Scholl

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).

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. :-(
 
R

Robert Klemme

T. Onoma said:
(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top