Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Conversion mask in hex to bit mask

Reply
Thread Tools

Conversion mask in hex to bit mask

 
 
Marcin Tyman
Guest
Posts: n/a
 
      05-06-2008
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/.

 
Reply With Quote
 
 
 
 
Todd Benson
Guest
Posts: n/a
 
      05-06-2008
On Tue, May 6, 2008 at 6:29 AM, Marcin Tyman <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      05-06-2008
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
 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      05-06-2008
On Tue, May 6, 2008 at 12:10 PM, Robert Klemme
<(E-Mail Removed)> 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

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      05-06-2008
On 06.05.2008 21:25, Todd Benson wrote:
> On Tue, May 6, 2008 at 12:10 PM, Robert Klemme
> <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit, Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new ! vvcd Computer Support 0 09-17-2004 08:15 PM
[networking] Convert subnet mask <=> mask length 187 Perl Misc 2 07-29-2004 10:31 AM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit,Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new! Ionizer Computer Support 1 01-01-2004 07:27 PM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments