PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > mysql.general > bitwise logic
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
bitwise logic

Réponse
 
LinkBack Outils de la discussion
Vieux 24/08/2007, 20h24   #1
Wagner, Chris
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut bitwise logic

Hi. I've got IP addresses stored in the database as packed binaries.
i.e. binary(4). I put them in there that way so that I could do bitwise
logic on them to do subnet searches. e.g. ...WHERE `ip` & 'mask' =
'network'. Only it turns out that as far as I can tell MySQL can't do
bit logic on strings, only integers. I came up with an onerous SQL call
that eats the 4 bytes and makes an integer out of them but there's got
to be a better way. The worst case is that I redo the database to have
all the IP's as integers.

Anybody know of a way to make MySQL either do the bit logic on the
string or convert/cast the string as an integer? Thanks.


--
Chris Wagner
CBTS
GE Aircraft Engines
Chris.Wagner@ae.ge.com
  Réponse avec citation
Vieux 28/08/2007, 15h35   #2
Wagner, Chris
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bitwise logic

Those functions concern dotted quad IP addresses, not packed binaries.

Anybody know of some obscure MySQL functions to do bit logic on strings
or get MySQL to recognize a char sequence as an integer?

"Gerald L. Clark" wrote:
>
> Wagner, Chris (GEAE, CBTS) wrote:
> > Hi. I've got IP addresses stored in the database as packed binaries.
> > i.e. binary(4). I put them in there that way so that I could do bitwise
> > logic on them to do subnet searches. e.g. ...WHERE `ip` & 'mask' =
> > 'network'. Only it turns out that as far as I can tell MySQL can't do
> > bit logic on strings, only integers. I came up with an onerous SQL call
> > that eats the 4 bytes and makes an integer out of them but there's got
> > to be a better way. The worst case is that I redo the database to have
> > all the IP's as integers.
> >
> > Anybody know of a way to make MySQL either do the bit logic on the
> > string or convert/cast the string as an integer? Thanks.
> >
> >

> Try INET_ATON() and INET_NTOA().
>
> --
> Gerald L. Clark
> Supplier Systems Corporation


--
Chris Wagner
CBTS
GE Aircraft Engines
Chris.Wagner@ae.ge.com
  Réponse avec citation
Vieux 28/08/2007, 16h40   #3
Baron Schwartz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bitwise logic

I think Gerald was suggesting you convert the IP's into integers and
then do bitwise stuff on them. I don't know of a way to do what you're
asking.

Baron

Wagner, Chris (GEAE, CBTS) wrote:
> Those functions concern dotted quad IP addresses, not packed binaries.
>
> Anybody know of some obscure MySQL functions to do bit logic on strings
> or get MySQL to recognize a char sequence as an integer?
>
> "Gerald L. Clark" wrote:
>> Wagner, Chris (GEAE, CBTS) wrote:
>>> Hi. I've got IP addresses stored in the database as packed binaries.
>>> i.e. binary(4). I put them in there that way so that I could do bitwise
>>> logic on them to do subnet searches. e.g. ...WHERE `ip` & 'mask' =
>>> 'network'. Only it turns out that as far as I can tell MySQL can't do
>>> bit logic on strings, only integers. I came up with an onerous SQL call
>>> that eats the 4 bytes and makes an integer out of them but there's got
>>> to be a better way. The worst case is that I redo the database to have
>>> all the IP's as integers.
>>>
>>> Anybody know of a way to make MySQL either do the bit logic on the
>>> string or convert/cast the string as an integer? Thanks.
>>>
>>>

>> Try INET_ATON() and INET_NTOA().
>>
>> --
>> Gerald L. Clark
>> Supplier Systems Corporation

>


--
Baron Schwartz
Xaprb LLC
http://www.xaprb.com/
  Réponse avec citation
Vieux 10/09/2007, 19h24   #4
Jeremy Cole
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bitwise logic

Hi,

You can actually unpack them using some fairly cryptic stuff. I would
only use this to unpack them once to re-store them as INT. Here's an
example:

CREATE TABLE ip (packed CHAR(4));
INSERT INTO ip (packed) VALUES (0xB16212C);

mysql> SELECT * FROM ip;
+--------+
| packed |
+--------+
|
!, |
+--------+
1 row in set (0.00 sec)

mysql> SELECT
-> INET_NTOA(
-> (ord(substring(packed, 1, 1)) << 24) +
-> (ord(substring(packed, 2, 2)) << 16) +
-> (ord(substring(packed, 3, 3)) << 8) +
-> (ord(substring(packed, 4, 4)))
-> ) AS unpacked
-> FROM ip;
+-------------+
| unpacked |
+-------------+
| 11.22.33.44 |
+-------------+
1 row in set (0.00 sec)

Regards,

Jeremy

Baron Schwartz wrote:
> I think Gerald was suggesting you convert the IP's into integers and
> then do bitwise stuff on them. I don't know of a way to do what you're
> asking.
>
> Baron
>
> Wagner, Chris (GEAE, CBTS) wrote:
>> Those functions concern dotted quad IP addresses, not packed binaries.
>>
>> Anybody know of some obscure MySQL functions to do bit logic on strings
>> or get MySQL to recognize a char sequence as an integer?
>>
>> "Gerald L. Clark" wrote:
>>> Wagner, Chris (GEAE, CBTS) wrote:
>>>> Hi. I've got IP addresses stored in the database as packed binaries.
>>>> i.e. binary(4). I put them in there that way so that I could do bitwise
>>>> logic on them to do subnet searches. e.g. ...WHERE `ip` & 'mask' =
>>>> 'network'. Only it turns out that as far as I can tell MySQL can't do
>>>> bit logic on strings, only integers. I came up with an onerous SQL call
>>>> that eats the 4 bytes and makes an integer out of them but there's got
>>>> to be a better way. The worst case is that I redo the database to have
>>>> all the IP's as integers.
>>>>
>>>> Anybody know of a way to make MySQL either do the bit logic on the
>>>> string or convert/cast the string as an integer? Thanks.
>>>>
>>>>
>>> Try INET_ATON() and INET_NTOA().
>>>
>>> --
>>> Gerald L. Clark
>>> Supplier Systems Corporation

>


--
high performance mysql consulting
www.provenscaling.com
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h47.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10277 seconds with 12 queries