|
I recently answered a with question with the code below. My question is, what is the best way to convert a binary [string] to int so I can remove one of the recursive ctes. Eg SELECT CAST('0011' as binary(1)) I am looking for 3 not 48
(comments are locked)
|
|
Substring works on binary values without converting to characters, so you can easily work out each portion of the IP address as you need using that. Don't ever put the binary code as a string, it's not worth it. Of course, if we had unsigned int available, it would be much easier. Binary(4) does the trick though, as this should work:
I haven't tested this today, as I'm writing this on my iPhone. Edit: The bit you were looking for was the discussion in the comments about power(2,32-N)-1 which is 00..000111.111 - which can be used with power(2,32)-1 to make something which is a subnet mask. Thanks for the interest Rob, what I'm trying to do is a little different. Think of it this way: I know I need a subnet mask of 20 bits (leaving 12 zeroed bits to the right) How do I get the bigint representation of that number in a more elegant manner than I came up with? Conceptually I can create this with a string of 20 1's and 12 0's then convert that but you're saying don't ever do that
Mar 20 '10 at 09:24 PM
Scot Hauder
I need a bigint since sql server will not allow a bitwise operation on 2 binary types--which seems like the natural thing to do. Perhaps you or Remus have the resources to find out why that decision was made by the sql dev team.
Mar 20 '10 at 09:37 PM
Scot Hauder
I don't think of a subnet mask as 1s and 0s in binary. I think of it as a power of 2, at which point you knock off the remainder. So, for a /29 (8 addresses), you consider this power(2, 32-29). You can easily construct this as 255.255.255 and 255-8-1.
Mar 20 '10 at 09:45 PM
Rob Farley
Oh, and when I was doing a lot of this, I would just leave it as four tinyints, since ordering was still easy, and I could easily apply masks in a variety of ways.
Mar 20 '10 at 09:49 PM
Rob Farley
Thank you that's what I was looking for! My train of thought was that of calculating the value of the set bits, but I can simply subtract the zeroed bit value. So my answer becomes: power(2,32)-power(2,32-N) obviating the need for the recursive cte. Cheers
Mar 21 '10 at 03:28 AM
Scot Hauder
(comments are locked)
|
|
Thanks Peso, I ended up going with this, it lets the user choose the variable bit mask. The title is a little misleading, I was looking for a way to calculate the bigint value of a bitmask if I knew the number of bits in the mask So, what is the difference in the output?
Mar 22 '10 at 07:02 PM
Peso
set vlsmsuffix = 23
Mar 22 '10 at 07:12 PM
Scot Hauder
So it's really a CDIR vs subnet mask thingy?
Mar 22 '10 at 07:22 PM
Peso
yes, so the user can specify the variable network portion of the address
Mar 22 '10 at 09:45 PM
Scot Hauder
(comments are locked)
|
|
You want simpler?
(comments are locked)
|
|
How about this (assuming you have a tally table/view called tally, which has a column called N): Thanks MrM, I am doing something similar in the recursive query, just I'm starting from the most significant bit and working to the right, dividing by 2. I guess I was hoping there was a more elegant solution without walking the binary string. Thank you for looking at it
Mar 20 '10 at 10:28 PM
Scot Hauder
(comments are locked)
|

