how to convert binary array into integer

Discussion in 'Ruby' started by hsun, Aug 31, 2005.

  1. hsun

    hsun Guest

    Hi,

    Does anyone know the api to convert a binary array into integer:

    for example: 00 00 00 0A =3D> 10

    thanks=20
     
    hsun, Aug 31, 2005
    #1
    1. Advertising

  2. hsun

    Joe Van Dyk Guest

    On 8/31/05, hsun <> wrote:
    > Hi,
    >=20
    > Does anyone know the api to convert a binary array into integer:
    >=20
    > for example: 00 00 00 0A =3D> 10


    Have you looked at String#unpack?
     
    Joe Van Dyk, Aug 31, 2005
    #2
    1. Advertising

  3. hsun

    Ara.T.Howard Guest

    On Thu, 1 Sep 2005, hsun wrote:

    > Hi,
    >
    > Does anyone know the api to convert a binary array into integer:
    >
    > for example: 00 00 00 0A => 10
    >
    > thanks


    harp:~ > cat a.rb
    p( [0x0, 0x0, 0x0, 0xA].pack('c*').unpack('N').first )

    harp:~ > ruby a.rb
    10

    hth.

    -a
    --
    ===============================================================================
    | email :: ara [dot] t [dot] howard [at] noaa [dot] gov
    | phone :: 303.497.6469
    | Your life dwells amoung the causes of death
    | Like a lamp standing in a strong breeze. --Nagarjuna
    ===============================================================================
     
    Ara.T.Howard, Aug 31, 2005
    #3
  4. hsun

    Zach Dennis Guest

    Joe Van Dyk wrote:
    > On 8/31/05, hsun <> wrote:
    >
    >>Hi,
    >>
    >>Does anyone know the api to convert a binary array into integer:
    >>
    >>for example: 00 00 00 0A => 10

    >
    >
    > Have you looked at String#unpack?



    "\000\000 A".unpack( "f" ) => 10.0

    Zach
     
    Zach Dennis, Aug 31, 2005
    #4
  5. Zach Dennis wrote:

    > Joe Van Dyk wrote:
    >
    >> On 8/31/05, hsun <> wrote:
    >>
    >>> Hi,
    >>>
    >>> Does anyone know the api to convert a binary array into integer:
    >>>
    >>> for example: 00 00 00 0A => 10

    >>
    >>
    >>
    >> Have you looked at String#unpack?

    >
    >
    >
    > "\000\000 A".unpack( "f" ) => 10.0
    >
    > Zach


    This was a joke, right?

    "\000\000 B".unpack( "f" ) => 40.0

    if not, i didn't want to be cynical.

    you can try this:

    a = [0x00,0x00,0x00,0x0A]
    puts a.inject(0){|r, i| r << 8 | i}

    cheers

    Simon
     
    Simon Kröger, Aug 31, 2005
    #5
    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. nick
    Replies:
    0
    Views:
    936
  2. nick
    Replies:
    1
    Views:
    32,090
    Eric Sosman
    Oct 26, 2004
  3. Dave
    Replies:
    16
    Views:
    728
    Dick de Boer
    Oct 26, 2005
  4. Replies:
    1
    Views:
    503
    Thomas J. Gritzan
    Jul 20, 2006
  5. Replies:
    4
    Views:
    1,423
Loading...

Share This Page