Binary numbers

Discussion in 'Ruby' started by Kim Pedersen, Aug 1, 2006.

  1. Kim Pedersen

    Kim Pedersen Guest

    Hi,

    I have some data with binary numbers.
    e.g. a 3 byte string could be 0x01216f which is 74095 decimal.
    To do this conversion in ruby I've come up with:

    def binum(bs)
    n = e = 0
    bs.reverse.each_byte do |c|
    n += c * 256 ** e
    e += 1
    end
    n
    end

    bs = ''
    bs << 0x01 << 0x21 << 0x6f

    puts binum(bs) => 74095

    Now some data are signed binary numbers stored as two's complement.
    e.g. 0xfede91 which is -74095 decimal.

    How can I do this conversion in ruby?

    Best regards,
    Kim
     
    Kim Pedersen, Aug 1, 2006
    #1
    1. Advertising

  2. On Wednesday, August 02, 2006, at 5:46 AM, Kim Pedersen wrote:
    >Hi,
    >
    >I have some data with binary numbers.
    >e.g. a 3 byte string could be 0x01216f which is 74095 decimal.
    >To do this conversion in ruby I've come up with:
    >
    >def binum(bs)
    > n = e = 0
    > bs.reverse.each_byte do |c|
    > n += c * 256 ** e
    > e += 1
    > end
    > n
    >end
    >
    >bs = ''
    >bs << 0x01 << 0x21 << 0x6f
    >
    >puts binum(bs) => 74095
    >
    >Now some data are signed binary numbers stored as two's complement.
    >e.g. 0xfede91 which is -74095 decimal.
    >
    >How can I do this conversion in ruby?


    def binum(bs)
    result = 0
    bs.unpack("cCC").each {|b| result = (result << 8) | b }
    result
    end

    bs = ''
    bs << 0x01 << 0x21 << 0x6f

    puts binum(bs)

    bs = ''
    bs << 0xfe << 0xde << 0x91

    puts binum(bs)

    The above code extracts the first byte as a signed 8-bit integer and the
    rest of the bytes as unsigned 8-bit integers. Each iteration through the
    loop shift the result to construct the proper value.

    --Dale Martenson



    --
    Posted with http://DevLists.com. Sign up and save your mailbox.
     
    Dale Martenson, Aug 1, 2006
    #2
    1. Advertising

  3. On Aug 1, 2006, at 4:33 PM, Dale Martenson wrote:

    > def binum(bs)
    > result = 0
    > bs.unpack("cCC").each {|b| result = (result << 8) | b }
    > result
    > end


    That's just crying out for inject():

    bs.unpack("cCC").inject(0) { |res, b| (res << 8) | b }

    ;)

    James Edward Gray II
     
    James Edward Gray II, Aug 1, 2006
    #3
  4. Kim Pedersen

    Kim Pedersen Guest

    Dale Martenson wrote:
    > On Wednesday, August 02, 2006, at 5:46 AM, Kim Pedersen wrote:
    >
    >> Hi,
    >>
    >> I have some data with binary numbers.
    >> e.g. a 3 byte string could be 0x01216f which is 74095 decimal.
    >> To do this conversion in ruby I've come up with:
    >>
    >> def binum(bs)
    >> n = e = 0
    >> bs.reverse.each_byte do |c|
    >> n += c * 256 ** e
    >> e += 1
    >> end
    >> n
    >> end
    >>
    >> bs = ''
    >> bs << 0x01 << 0x21 << 0x6f
    >>
    >> puts binum(bs) => 74095
    >>
    >> Now some data are signed binary numbers stored as two's complement.
    >> e.g. 0xfede91 which is -74095 decimal.
    >>
    >> How can I do this conversion in ruby?
    >>

    >
    > def binum(bs)
    > result = 0
    > bs.unpack("cCC").each {|b| result = (result << 8) | b }
    > result
    > end
    >
    > bs = ''
    > bs << 0x01 << 0x21 << 0x6f
    >
    > puts binum(bs)
    >
    > bs = ''
    > bs << 0xfe << 0xde << 0x91
    >
    > puts binum(bs)
    >
    > The above code extracts the first byte as a signed 8-bit integer and the
    > rest of the bytes as unsigned 8-bit integers. Each iteration through the
    > loop shift the result to construct the proper value.
    >
    > --Dale Martenson
    >

    Great!
    Thanks
    Kim

    BTW, the length of the string may vary from 1 to 8. I'll have to adjust
    for that.
     
    Kim Pedersen, Aug 1, 2006
    #4
  5. Kim Pedersen

    Kim Pedersen Guest

    James Edward Gray II wrote:
    > On Aug 1, 2006, at 4:33 PM, Dale Martenson wrote:
    >
    >> def binum(bs)
    >> result = 0
    >> bs.unpack("cCC").each {|b| result = (result << 8) | b }
    >> result
    >> end

    >
    > That's just crying out for inject():
    >
    > bs.unpack("cCC").inject(0) { |res, b| (res << 8) | b }
    >
    > ;)
    >
    > James Edward Gray II

    Thanks,
    ps. just bought your ruby quiz book :)
     
    Kim Pedersen, Aug 1, 2006
    #5
  6. On 8/2/06, Kim Pedersen <> wrote:
    > Dale Martenson wrote:
    > > On Wednesday, August 02, 2006, at 5:46 AM, Kim Pedersen wrote:
    > >
    > >> Hi,
    > >>
    > >> I have some data with binary numbers.
    > >> e.g. a 3 byte string could be 0x01216f which is 74095 decimal.
    > >> To do this conversion in ruby I've come up with:
    > >>
    > >> def binum(bs)
    > >> n = e = 0
    > >> bs.reverse.each_byte do |c|
    > >> n += c * 256 ** e
    > >> e += 1
    > >> end
    > >> n
    > >> end
    > >>
    > >> bs = ''
    > >> bs << 0x01 << 0x21 << 0x6f
    > >>
    > >> puts binum(bs) => 74095
    > >>
    > >> Now some data are signed binary numbers stored as two's complement.
    > >> e.g. 0xfede91 which is -74095 decimal.
    > >>
    > >> How can I do this conversion in ruby?
    > >>

    > >
    > > def binum(bs)
    > > result = 0
    > > bs.unpack("cCC").each {|b| result = (result << 8) | b }
    > > result
    > > end
    > >
    > > bs = ''
    > > bs << 0x01 << 0x21 << 0x6f
    > >
    > > puts binum(bs)
    > >
    > > bs = ''
    > > bs << 0xfe << 0xde << 0x91
    > >
    > > puts binum(bs)
    > >
    > > The above code extracts the first byte as a signed 8-bit integer and the
    > > rest of the bytes as unsigned 8-bit integers. Each iteration through the
    > > loop shift the result to construct the proper value.
    > >
    > > --Dale Martenson
    > >

    > Great!
    > Thanks
    > Kim
    >
    > BTW, the length of the string may vary from 1 to 8. I'll have to adjust
    > for that.


    For variable length;

    c.unpack("cC*").inject(0) { |res, b| (res << 8) | b }
     
    David Balmain, Aug 2, 2006
    #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. Subra
    Replies:
    25
    Views:
    1,257
    user923005
    Mar 8, 2007
  2. Andrew Tatum

    Fibonacci Numbers and Lucas Numbers

    Andrew Tatum, May 26, 2007, in forum: C++
    Replies:
    6
    Views:
    608
    Howard
    May 27, 2007
  3. Lance Hoffmeyer
    Replies:
    2
    Views:
    550
    Lance Hoffmeyer
    Jul 26, 2007
  4. jko170
    Replies:
    9
    Views:
    138
    jko170
    Jan 21, 2009
  5. GIMME
    Replies:
    5
    Views:
    200
    Thomas 'PointedEars' Lahn
    Jul 26, 2004
Loading...

Share This Page