Conversion mask in hex to bit mask

Discussion in 'Ruby' started by Marcin Tyman, May 6, 2008.

  1. Marcin Tyman

    Marcin Tyman Guest

    Hi,
    The issue is: how to convert hex mask to bit mask? For instance:

    I have 0xffff0000 -> it can be represent by 255.255.0.0 or by string
    of bits - 16

    My question is how to convert such hexes to bit mask (the 16 in above
    example)


    Thanks in advance,
    Have a nice day
    --
    Posted via http://www.ruby-forum.com/.
     
    Marcin Tyman, May 6, 2008
    #1
    1. Advertising

  2. Marcin Tyman

    Todd Benson Guest

    On Tue, May 6, 2008 at 6:29 AM, Marcin Tyman <> wrote:
    > Hi,
    > The issue is: how to convert hex mask to bit mask? For instance:
    >
    > I have 0xffff0000 -> it can be represent by 255.255.0.0 or by string
    > of bits - 16
    >
    > My question is how to convert such hexes to bit mask (the 16 in above
    > example)
    >
    >
    > Thanks in advance,
    > Have a nice day


    irb(main):001:0> 0xffff0000.to_s(2) =~ /0*$/
    => 16

    Untested for other masks.

    hth,
    Todd
     
    Todd Benson, May 6, 2008
    #2
    1. Advertising

  3. On 06.05.2008 13:29, Marcin Tyman wrote:
    > The issue is: how to convert hex mask to bit mask? For instance:
    >
    > I have 0xffff0000 -> it can be represent by 255.255.0.0 or by string
    > of bits - 16


    Not sure what exactly you mean by this. Do you want to count 1's?

    Here are some things you can do:

    irb(main):001:0> c = 0xffff0000
    => 4294901760
    irb(main):002:0> c.to_s(2)
    => "11111111111111110000000000000000"
    irb(main):003:0> c.to_s(2).length
    => 32

    Bit access:

    irb(main):004:0> c[0]
    => 0
    irb(main):005:0> c[20]
    => 1

    > My question is how to convert such hexes to bit mask (the 16 in above
    > example)


    Cheers

    robert
     
    Robert Klemme, May 6, 2008
    #3
  4. Marcin Tyman

    Todd Benson Guest

    On Tue, May 6, 2008 at 12:10 PM, Robert Klemme
    <> wrote:
    > On 06.05.2008 13:29, Marcin Tyman wrote:
    >
    > > The issue is: how to convert hex mask to bit mask? For instance:
    > >
    > > I have 0xffff0000 -> it can be represent by 255.255.0.0 or by string
    > > of bits - 16
    > >

    >
    > Not sure what exactly you mean by this. Do you want to count 1's?


    I think Marcin wants to eventually convert/print out to shorthand the
    representation of an IPV4 address, like how "192.168.1.9/24" means
    "192.168.1.9 with netmask 255.255.255.0", /8 means a 255.0.0.0 mask,
    etc.

    That was my interpretation anyway.

    Todd
    Todd
     
    Todd Benson, May 6, 2008
    #4
  5. On 06.05.2008 21:25, Todd Benson wrote:
    > On Tue, May 6, 2008 at 12:10 PM, Robert Klemme
    > <> wrote:
    >> On 06.05.2008 13:29, Marcin Tyman wrote:
    >>
    >>> The issue is: how to convert hex mask to bit mask? For instance:
    >>>
    >>> I have 0xffff0000 -> it can be represent by 255.255.0.0 or by string
    >>> of bits - 16
    >>>

    >> Not sure what exactly you mean by this. Do you want to count 1's?

    >
    > I think Marcin wants to eventually convert/print out to shorthand the
    > representation of an IPV4 address, like how "192.168.1.9/24" means
    > "192.168.1.9 with netmask 255.255.255.0", /8 means a 255.0.0.0 mask,
    > etc.
    >
    > That was my interpretation anyway.


    Sounds reasonable. Marcin?

    robert
     
    Robert Klemme, May 6, 2008
    #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. Replies:
    10
    Views:
    6,223
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    474
    Juha Autero
    Aug 19, 2003
  3. Replies:
    3
    Views:
    1,767
    Timothy Bendfelt
    Jan 19, 2007
  4. Replies:
    9
    Views:
    982
    Juha Nieminen
    Aug 22, 2007
  5. 187
    Replies:
    2
    Views:
    566
    Bart Lateur
    Jul 29, 2004
Loading...

Share This Page