character to hex/binary/etc...

Discussion in 'Ruby' started by John Joyce, Aug 16, 2007.

  1. John Joyce

    John Joyce Guest

    Hmm... I can turn a number into a character with .chr
    I can convert my numbers with .to_s(2) ((where 2 is a radix, or base))
    But how do I convert numbers to characters??
     
    John Joyce, Aug 16, 2007
    #1
    1. Advertising

  2. John Joyce wrote:
    > Hmm... I can turn a number into a character with .chr
    > I can convert my numbers with .to_s(2) ((where 2 is a radix, or base))
    > But how do I convert numbers to characters??


    I don't understand what you mean - "to_s(b)" builds a string. What do
    you mean by "convert numbers to characters" (example).

    Wolfgang Nádasi-Donner
    --
    Posted via http://www.ruby-forum.com/.
     
    Wolfgang Nádasi-Donner, Aug 16, 2007
    #2
    1. Advertising

  3. John Joyce

    John Joyce Guest

    On Aug 16, 2007, at 2:57 PM, Wolfgang N=E1dasi-Donner wrote:

    > John Joyce wrote:
    >> Hmm... I can turn a number into a character with .chr
    >> I can convert my numbers with .to_s(2) ((where 2 is a radix, or =20
    >> base))
    >> But how do I convert numbers to characters??

    >
    > I don't understand what you mean - "to_s(b)" builds a string. What do
    > you mean by "convert numbers to characters" (example).
    >
    > Wolfgang N=E1dasi-Donner
    > --=20
    > Posted via http://www.ruby-forum.com/.
    >

    yes it builds a string representation of a number.
    I want to take a hex or binary number though and return a character.
    0x49 for example is "I"
     
    John Joyce, Aug 16, 2007
    #3
  4. John Joyce wrote:
    > On Aug 16, 2007, at 2:57 PM, Wolfgang N�dasi-Donner wrote:
    >
    >> --
    >> Posted via http://www.ruby-forum.com/.
    >>

    > yes it builds a string representation of a number.
    > I want to take a hex or binary number though and return a character.
    > 0x49 for example is "I"


    irb(main):002:0> 0x49.chr
    => "I"

    It works!
    --
    Posted via http://www.ruby-forum.com/.
     
    Wolfgang Nádasi-Donner, Aug 16, 2007
    #4
  5. Alle gioved=EC 16 agosto 2007, John Joyce ha scritto:
    > On Aug 16, 2007, at 2:57 PM, Wolfgang N=E1dasi-Donner wrote:
    > > John Joyce wrote:
    > >> Hmm... I can turn a number into a character with .chr
    > >> I can convert my numbers with .to_s(2) ((where 2 is a radix, or
    > >> base))
    > >> But how do I convert numbers to characters??

    > >
    > > I don't understand what you mean - "to_s(b)" builds a string. What do
    > > you mean by "convert numbers to characters" (example).
    > >
    > > Wolfgang N=E1dasi-Donner
    > > --
    > > Posted via http://www.ruby-forum.com/.

    >
    > yes it builds a string representation of a number.
    > I want to take a hex or binary number though and return a character.
    > 0x49 for example is "I"


    I think you need Integer#chr:

    0x49.chr
    =3D> 'I'

    Stefano
     
    Stefano Crocco, Aug 16, 2007
    #5
  6. John Joyce

    Tom Werner Guest

    John Joyce wrote:
    > Hmm... I can turn a number into a character with .chr
    > I can convert my numbers with .to_s(2) ((where 2 is a radix, or base))
    > But how do I convert numbers to characters??
    >
    >


    You may be looking for Array#pack. It's useful for converting an array
    from one encoding to another. One of these possibilities is to convert
    an array of numbers into a string:

    n = [65, 66, 67]
    n.pack("c*")
    #=> "ABC"


    Pack (and unpack) can do tons of useful and surprising things.

    Tom

    --
    * Libraries:
    Chronic (chronic.rubyforge.org)
    God (god.rubyforge.org)
    * Site:
    rubyisawesome.com
     
    Tom Werner, Aug 16, 2007
    #6
  7. John Joyce

    John Joyce Guest

    On Aug 16, 2007, at 3:22 PM, Stefano Crocco wrote:

    > Alle gioved=EC 16 agosto 2007, John Joyce ha scritto:
    >> On Aug 16, 2007, at 2:57 PM, Wolfgang N=E1dasi-Donner wrote:
    >>> John Joyce wrote:
    >>>> Hmm... I can turn a number into a character with .chr
    >>>> I can convert my numbers with .to_s(2) ((where 2 is a radix, or
    >>>> base))
    >>>> But how do I convert numbers to characters??
    >>>
    >>> I don't understand what you mean - "to_s(b)" builds a string. =20
    >>> What do
    >>> you mean by "convert numbers to characters" (example).
    >>>
    >>> Wolfgang N=E1dasi-Donner
    >>> --
    >>> Posted via http://www.ruby-forum.com/.

    >>
    >> yes it builds a string representation of a number.
    >> I want to take a hex or binary number though and return a character.
    >> 0x49 for example is "I"

    >
    > I think you need Integer#chr:
    >
    > 0x49.chr
    > =3D> 'I'
    >
    > Stefano
    >

    Oops, no, that's not what I need. I can do that.
    I need to take "I" or "J" or whatever character and convert to hex! =20
    or binary
    (sorry, I'm a little sleepy now)=
     
    John Joyce, Aug 16, 2007
    #7
  8. John Joyce

    Adam Shelly Guest

    On 8/16/07, John Joyce <> wrote:
    > Oops, no, that's not what I need. I can do that.
    > I need to take "I" or "J" or whatever character and convert to hex!
    > or binary
    > (sorry, I'm a little sleepy now)
    >

    like this?


    irb(main):011:0> s = "A"
    => "A"
    irb(main):012:0> s[0]
    => 65
    irb(main):013:0> c = ?A
    => 65
    irb(main):014:0> s[0].to_s(16)
    => "41"
    irb(main):015:0> c.to_s(16)
    => "41"
    irb(main):016:0> c.to_s(2)
    => "1000001"
     
    Adam Shelly, Aug 16, 2007
    #8
  9. John Joyce

    Tom Werner Guest

    John Joyce wrote:
    >
    > On Aug 16, 2007, at 3:22 PM, Stefano Crocco wrote:
    >
    >> Alle giovedì 16 agosto 2007, John Joyce ha scritto:
    >>> On Aug 16, 2007, at 2:57 PM, Wolfgang Nádasi-Donner wrote:
    >>>> John Joyce wrote:
    >>>>> Hmm... I can turn a number into a character with .chr
    >>>>> I can convert my numbers with .to_s(2) ((where 2 is a radix, or
    >>>>> base))
    >>>>> But how do I convert numbers to characters??
    >>>>
    >>>> I don't understand what you mean - "to_s(b)" builds a string. What do
    >>>> you mean by "convert numbers to characters" (example).
    >>>>
    >>>> Wolfgang Nádasi-Donner
    >>>> --
    >>>> Posted via http://www.ruby-forum.com/.
    >>>
    >>> yes it builds a string representation of a number.
    >>> I want to take a hex or binary number though and return a character.
    >>> 0x49 for example is "I"

    >>
    >> I think you need Integer#chr:
    >>
    >> 0x49.chr
    >> => 'I'
    >>
    >> Stefano
    >>

    > Oops, no, that's not what I need. I can do that.
    > I need to take "I" or "J" or whatever character and convert to hex! or
    > binary
    > (sorry, I'm a little sleepy now)
    >


    Ah well in that case:

    ?I
    # => 73

    ?I.to_s(2)
    # => "1001001"

    ?I.to_s(16)
    # => "49"

    Tom Preston-Werner

    --
    * Libraries:
    Chronic (chronic.rubyforge.org)
    God (god.rubyforge.org)
    * Site:
    rubyisawesome.com
     
    Tom Werner, Aug 16, 2007
    #9
  10. John Joyce

    John Joyce Guest

    On Aug 16, 2007, at 3:36 PM, Adam Shelly wrote:

    > On 8/16/07, John Joyce <> wrote:
    >> Oops, no, that's not what I need. I can do that.
    >> I need to take "I" or "J" or whatever character and convert to hex!
    >> or binary
    >> (sorry, I'm a little sleepy now)
    >>

    > like this?
    >
    >
    > irb(main):011:0> s = "A"
    > => "A"
    > irb(main):012:0> s[0]
    > => 65
    > irb(main):013:0> c = ?A
    > => 65
    > irb(main):014:0> s[0].to_s(16)
    > => "41"
    > irb(main):015:0> c.to_s(16)
    > => "41"
    > irb(main):016:0> c.to_s(2)
    > => "1000001"
    >

    Hmm... that first technique is useful! Returning elements of a string.
     
    John Joyce, Aug 16, 2007
    #10
  11. John Joyce

    John Joyce Guest

    On Aug 16, 2007, at 3:27 PM, Tom Werner wrote:

    > John Joyce wrote:
    >> Hmm... I can turn a number into a character with .chr
    >> I can convert my numbers with .to_s(2) ((where 2 is a radix, or
    >> base))
    >> But how do I convert numbers to characters??
    >>
    >>

    >
    > You may be looking for Array#pack. It's useful for converting an
    > array from one encoding to another. One of these possibilities is
    > to convert an array of numbers into a string:
    >
    > n = [65, 66, 67]
    > n.pack("c*")
    > #=> "ABC"
    >
    >
    > Pack (and unpack) can do tons of useful and surprising things.
    >
    > Tom
    >

    The creator of god! I must listen carefully.

    I after some reading, I'm starting to think about using the files
    signatures used by DROID / PRONOM
    Anyone know much about that stuff? Any Ruby implementations/bindings?
     
    John Joyce, Aug 16, 2007
    #11
  12. * John Joyce <> (22:50) schrieb:

    > Hmm... that first technique is useful! Returning elements of a string.


    Gut that won't work in Ruby 1.9. "haha"[0] is 104 in 1.8 but "h" in 1.9.
    You have to use "haha".bytes.first in Ruby 1.9, but that won't work in
    1.8.

    mfg, simon .... l
     
    Simon Krahnke, Aug 17, 2007
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,281
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    489
    Juha Autero
    Aug 19, 2003
  3. jack
    Replies:
    4
    Views:
    596
  4. Kevin Walzer

    Re: PIL (etc etc etc) on OS X

    Kevin Walzer, Aug 1, 2008, in forum: Python
    Replies:
    4
    Views:
    431
    Fredrik Lundh
    Aug 13, 2008
  5. Replies:
    7
    Views:
    3,641
Loading...

Share This Page