Convert a char into numbre

Discussion in 'Ruby' started by Roberto Decurnex Gorosito, Dec 18, 2007.

  1. I am have an String object like "12345678", and an Array object like
    [ 8,7,6,5,4,3,2,1].
    The main idea is:

    myStr = '12345678'
    myArray = [8,7,6,5,4,3,2,1]
    total = 0

    for i in 0..7
    total+= myStr[ i ].to_i * myArray[ i ]
    end


    The awaited result is 120 but coz myStr[ i ] returns the ascii fixnum
    i can' t get it

    Like a quick solution i have to do this:

    myStr = '12345678'
    myArray = [8,7,6,5,4,3,2,1]
    total = 0

    for i in 0..7
    total += ( String.new << myStr[ i ] ).to_i * myArray[ i ]
    end

    It's so horrible!!!!! :(

    Please, help my code to be beautiful as ruby is
    Roberto Decurnex Gorosito, Dec 18, 2007
    #1
    1. Advertising

  2. Roberto Decurnex Gorosito

    Tim Hunter Guest

    Roberto Decurnex Gorosito wrote:
    > I am have an String object like "12345678", and an Array object like
    > [ 8,7,6,5,4,3,2,1].
    > The main idea is:
    >
    > myStr = '12345678'
    > myArray = [8,7,6,5,4,3,2,1]
    > total = 0
    >
    > for i in 0..7
    > total+= myStr[ i ].to_i * myArray[ i ]
    > end
    >
    >
    > The awaited result is 120 but coz myStr[ i ] returns the ascii fixnum
    > i can' t get it


    Try myStr[i,1]

    --
    RMagick: http://rmagick.rubyforge.org/
    Tim Hunter, Dec 18, 2007
    #2
    1. Advertising

  3. Roberto Decurnex Gorosito

    yermej Guest

    On Dec 18, 8:35 am, Roberto Decurnex Gorosito
    <> wrote:
    > I am have an String object like "12345678", and an Array object like
    > [ 8,7,6,5,4,3,2,1].
    > The main idea is:
    >
    > myStr = '12345678'
    > myArray = [8,7,6,5,4,3,2,1]
    > total = 0
    >
    > for i in 0..7
    > total+= myStr[ i ].to_i * myArray[ i ]
    > end
    >
    > The awaited result is 120 but coz myStr[ i ] returns the ascii fixnum
    > i can' t get it


    Fixnum#chr will give you the String back and then you can use
    String#to_i:

    total = 0
    myArray.each_with_index do |x, i|
    total += x * myStr.chr.to_i
    end
    total
    => 120

    Alternately, you could just subtract 48 from myStr if you'll always
    be using ASCII, but I don't think that's a good way to do things.

    As a one-liner:
    myStr.split(//).zip(myArray).inject(0) {|acc, arr| acc + arr[0].to_i *
    arr[1]}
    => 120
    yermej, Dec 18, 2007
    #3
  4. [Note: parts of this message were removed to make it a legal post.]

    You are right, thanks!

    On Dec 18, 2007 12:01 PM, Tim Hunter <> wrote:

    > Roberto Decurnex Gorosito wrote:
    > > I am have an String object like "12345678", and an Array object like
    > > [ 8,7,6,5,4,3,2,1].
    > > The main idea is:
    > >
    > > myStr = '12345678'
    > > myArray = [8,7,6,5,4,3,2,1]
    > > total = 0
    > >
    > > for i in 0..7
    > > total+= myStr[ i ].to_i * myArray[ i ]
    > > end
    > >
    > >
    > > The awaited result is 120 but coz myStr[ i ] returns the ascii fixnum
    > > i can' t get it

    >
    > Try myStr[i,1]
    >
    > --
    > RMagick: http://rmagick.rubyforge.org/
    >
    >
    Roberto Eduardo Decurnex Gorosito, Dec 18, 2007
    #4
  5. Roberto Decurnex Gorosito

    luis lopez Guest

    [Note: parts of this message were removed to make it a legal post.]

    roberto te llego?
    lo que tenes que hacer es , biceps y espalada hoy


    2007/12/18, yermej <>:
    >
    > On Dec 18, 8:35 am, Roberto Decurnex Gorosito
    > <> wrote:
    > > I am have an String object like "12345678", and an Array object like
    > > [ 8,7,6,5,4,3,2,1].
    > > The main idea is:
    > >
    > > myStr = '12345678'
    > > myArray = [8,7,6,5,4,3,2,1]
    > > total = 0
    > >
    > > for i in 0..7
    > > total+= myStr[ i ].to_i * myArray[ i ]
    > > end
    > >
    > > The awaited result is 120 but coz myStr[ i ] returns the ascii fixnum
    > > i can' t get it

    >
    > Fixnum#chr will give you the String back and then you can use
    > String#to_i:
    >
    > total = 0
    > myArray.each_with_index do |x, i|
    > total += x * myStr.chr.to_i
    > end
    > total
    > => 120
    >
    > Alternately, you could just subtract 48 from myStr if you'll always
    > be using ASCII, but I don't think that's a good way to do things.
    >
    > As a one-liner:
    > myStr.split(//).zip(myArray).inject(0) {|acc, arr| acc + arr[0].to_i *
    > arr[1]}
    > => 120
    >
    >
    luis lopez, Dec 18, 2007
    #5
  6. [Note: parts of this message were removed to make it a legal post.]

    That's what i was looking for chr, i just forget that one. Thanks!

    On Dec 18, 2007 12:05 PM, yermej <> wrote:

    > On Dec 18, 8:35 am, Roberto Decurnex Gorosito
    > <> wrote:
    > > I am have an String object like "12345678", and an Array object like
    > > [ 8,7,6,5,4,3,2,1].
    > > The main idea is:
    > >
    > > myStr = '12345678'
    > > myArray = [8,7,6,5,4,3,2,1]
    > > total = 0
    > >
    > > for i in 0..7
    > > total+= myStr[ i ].to_i * myArray[ i ]
    > > end
    > >
    > > The awaited result is 120 but coz myStr[ i ] returns the ascii fixnum
    > > i can' t get it

    >
    > Fixnum#chr will give you the String back and then you can use
    > String#to_i:
    >
    > total = 0
    > myArray.each_with_index do |x, i|
    > total += x * myStr.chr.to_i
    > end
    > total
    > => 120
    >
    > Alternately, you could just subtract 48 from myStr if you'll always
    > be using ASCII, but I don't think that's a good way to do things.
    >
    > As a one-liner:
    > myStr.split(//).zip(myArray).inject(0) {|acc, arr| acc + arr[0].to_i *
    > arr[1]}
    > => 120
    >
    >
    Roberto Eduardo Decurnex Gorosito, Dec 18, 2007
    #6
    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. wwj
    Replies:
    7
    Views:
    548
  2. wwj
    Replies:
    24
    Views:
    2,505
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    473
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    579
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,043
    Ian Collins
    May 9, 2006
Loading...

Share This Page