I've been playing with characters (OK, there's no such type in
Ruby, it's rather like characters in C - just plain integer numbers).
String of length 1 to a char: str1[0] --> chr_int
Char to a String of length 1: chr_int.chr --> str1
Get a char from a string: str[n] --> chr_int # or nil
Put a char into a string: str[n] = chr_int
So we can think of a "char" as a single element of a string.
Now, having that an integer is a single element of an array,
we'd like to have functions/expressions for converting arrays
of integers and strings back and forth.
Array to String: [97,98,99].map{|e|e.chr}.join --> "abc"
String to Array: "abc" ??? --> [97,98,99]
il Sat, 05 Jun 2004 10:40:45 GMT, "Georgy" <see@the.end> ha scritto::
Hi,
So we can think of a "char" as a single element of a string.
Now, having that an integer is a single element of an array,
we'd like to have functions/expressions for converting arrays
of integers and strings back and forth.
Array to String: [97,98,99].map{|e|e.chr}.join --> "abc"
String to Array: "abc" ??? --> [97,98,99]
"Georgy" <see@the.end> schrieb im Newsbeitrag
news:Nqhwc.15070$tH1.434991@twister.southeast.rr.com...
Hi,
I've been playing with characters (OK, there's no such type in
Ruby, it's rather like characters in C - just plain integer numbers).
String of length 1 to a char: str1[0] --> chr_int
Char to a String of length 1: chr_int.chr --> str1
Get a char from a string: str[n] --> chr_int # or nil
Put a char into a string: str[n] = chr_int
So we can think of a "char" as a single element of a string.
Now, having that an integer is a single element of an array,
we'd like to have functions/expressions for converting arrays
of integers and strings back and forth.
Array to String: [97,98,99].map{|e|e.chr}.join --> "abc"
This is more efficient:
[97,98,99].inject(""){|str,ch|str<<ch}
But of course the pack / unpack pair is the most appropriate way to do it as
others told already.