How to convert character code to a String? 
Author Message
 How to convert character code to a String?

Hi there,

I fear I might be missing something obvious, but I'm wondering how to
get a one-character String with a given character code.  By examining
the pickaxe book and the archive of this mailing list, I've found this
so far:

        c = 123
        s = String.new
        s << c
        list_of_strings.push(s)

This is a bit verbose, and I hope I can do something like the
following instead of the above:

        c = 123
        list_of_strings.push(char(c))

It's relatively easy to obtain character codes (String#[]), so I
expect there's a straight forward way to obtain a String from a
character code.  If there's no built-in way, that's OK, and I'll
define a method like "char" above or add an method to the Fixnum
class.

Thanks,
Ryo



Thu, 24 Jun 2004 22:19:19 GMT  
 How to convert character code to a String?

Quote:

> I fear I might be missing something obvious, but I'm wondering how to
> get a one-character String with a given character code.

Integer#chr is what you need:

------------------------------------------------------------ Integer#chr
     int.chr -> aString
------------------------------------------------------------------------
     Returns a string containing the ASCII character represented by the
     receiver's value.
        65.chr    #=> "A"
        ?a.chr    #=> "a"
        230.chr   #=> "\346"

--
<[ Kent Dahl ]>================<[ http://www.stud.ntnu.no/~kentda/ ]>
  )____(stud.techn.;ind.?k.data)||(softwareDeveloper.at(Trustix))_(
 /"Opinions expressed are mine and not those of my Employer,      "\
( "the University, my girlfriend, stray cats, banana fruitflies,  " )
 \"nor the frontal lobe of my left cerebral hemisphere.           "/



Fri, 25 Jun 2004 04:40:03 GMT  
 How to convert character code to a String?
Hi,

In message "[ruby-talk:30347] How to convert character code to a String?"

|expect there's a straight forward way to obtain a String from a
|character code.  If there's no built-in way, that's OK, and I'll
|define a method like "char" above or add an method to the Fixnum
|class.

Fixnum#chr

                                                        matz.



Fri, 25 Jun 2004 08:13:12 GMT  
 How to convert character code to a String?

I cannot get the connect method in fxruby to work.  The fxruby manual says:

------------ begin quote from manual ----------
.. This new model was introduced with the FXRuby-0.99.179 release ...
Event Model
FXRuby implements a new, simplified approach to this built on top of the old
model. It more or less mimics the syntax used in Ruby/GTK; you can attach a
message handler block to a widget using a new connect instance method, e.g.

    aButton = FXButton.new(parent, "Push Me")
    aButton.connect(SEL_COMMAND) { |sender, sel, ptr|
      puts "Ouch!"
    }
..
------------ end quote from manual ----------

I am running FXRuby-0.99.181-win32.exe with a cygwin Ruby.  The following
code is a combination of the above snippet with the "hello world" snippet in
the same manual:

require 'fox'
require 'fox/responder'
include Fox
include Responder

app = FXApp.new("Hello", "FoxTest")
app.init(ARGV)
main = FXMainWindow.new(app, "Hello", nil, nil, DECOR_ALL)
FXButton.new(main, "Push Me").connect(SEL_COMMAND) { puts "Ouch!" }
app.create
main.show(PLACEMENT_SCREEN)
app.run

This code gives the error: "undefined method `connect' for Push
Me:Fox::FXButton (NameError)".  If I remove the .connect call and block, the
button appears properly in the window.

Help please?



Fri, 25 Jun 2004 10:20:50 GMT  
 How to convert character code to a String?

Quote:

> Hi,

> In message "[ruby-talk:30347] How to convert character code to a String?"

> |expect there's a straight forward way to obtain a String from a
> |character code.  If there's no built-in way, that's OK, and I'll
> |define a method like "char" above or add an method to the Fixnum
> |class.

> Fixnum#chr

Thank you, Kent and Matz!  Yes, I was missing something
(semi-)obvious.

As Kent said, it's Integer#chr.  Of course, Integer is a superclass of
Fixnum....  Just out of curiosity, this makes me wonder why it isn't
Fixnum#chr.  Is that because you envision a future where Ruby handles
four-byte characters (assuming a Fixnum is 31 bits or longer)?

Finally, a feeble excuse :-)  I didn't find #chr because I checked
Fixnum but not Integer.  This is the reason why I said
"(semi-)obvious".

BTW, I bought a copy of "Ruby in a Nutshell" today.  The book is a
nice handy reference.  I'm happy with it.

Thanks again,
Ryo



Fri, 25 Jun 2004 21:48:07 GMT  
 How to convert character code to a String?

Quote:
> This code gives the error: "undefined method `connect' for Push
> Me:Fox::FXButton (NameError)".  If I remove the .connect call and block,
the
> button appears properly in the window.

Is there any chance that you installed the new version of FXRuby (i.e.
version 0.99.181) to some directory other than the Ruby installation
directory, and that you're actually referencing an older version of FXRuby
somewhere else on the disk? I just blew away my old copy of FXRuby, i.e. the
files:

    C:\ruby\lib\ruby\site_ruby\1.6\i386-cygwin\fox.so
    C:\ruby\lib\ruby\site_ruby\1.6\fox\*.rb

and then reinstalled FXRuby-0.99.181-win32.exe and your example worked as
expected.



Fri, 25 Jun 2004 23:36:22 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Convert Character String to Hex

2. Converting a number in a String character to an Integer

3. How do I convert String to Character?

4. convert integer counter to character string

5. convert character string to numeric value

6. Converting character to string

7. converting character strings to real numbers

8. How to convert integer to character (string) ?

9. Convert type String to List of single Characters

10. Converting ASCII characters <-> binary string

11. convert string to hex characters in TCL

12. Summary: convert character to ASCII code?

 

 
Powered by phpBB® Forum Software