SQL: Binary to IP Address
I'm trying to convert A binary IP to a human-readable IP
SELECT HEX( `ip_bin` ) FROM `log_metadata`
gives me
4333D26E000000000000000000000000
And
SELECT INET_NTOA(0x4333D26E)
gives me 67.51.210.110
So I tried:
SELECT
SUBSTRING( CONVERT(HEX(`ip_bin`), CHAR(32)), 1, 8 ) AS `A`
, INET_NTOA(
SUBSTRING( CONVERT(HEX(`ip_bin`), CHAR(32)), 1, 8 )
) AS `B`
, INET_NTOA(hex(`ip_bin`)) AS `C`
, INET_NTOA(`ip_bin`) AS `D`
FROM `log_metadata`
But I only get
+----------+------------+------------+---------+
| A | B | C | D |
+----------+------------+------------+---------+
| 4333D26E | 0.0.16.237 | 0.0.16.237 | 0.0.0.0 |
+----------+------------+------------+---------+
Any suggestions?
mysql string binary string-formatting
add a comment |
I'm trying to convert A binary IP to a human-readable IP
SELECT HEX( `ip_bin` ) FROM `log_metadata`
gives me
4333D26E000000000000000000000000
And
SELECT INET_NTOA(0x4333D26E)
gives me 67.51.210.110
So I tried:
SELECT
SUBSTRING( CONVERT(HEX(`ip_bin`), CHAR(32)), 1, 8 ) AS `A`
, INET_NTOA(
SUBSTRING( CONVERT(HEX(`ip_bin`), CHAR(32)), 1, 8 )
) AS `B`
, INET_NTOA(hex(`ip_bin`)) AS `C`
, INET_NTOA(`ip_bin`) AS `D`
FROM `log_metadata`
But I only get
+----------+------------+------------+---------+
| A | B | C | D |
+----------+------------+------------+---------+
| 4333D26E | 0.0.16.237 | 0.0.16.237 | 0.0.0.0 |
+----------+------------+------------+---------+
Any suggestions?
mysql string binary string-formatting
Probably is the missing0x
in front of the stringA
.
– Niloct
Mar 12 '13 at 19:51
Tried that. It just returned0
and0.0.0.0
– redolent
Mar 12 '13 at 19:54
add a comment |
I'm trying to convert A binary IP to a human-readable IP
SELECT HEX( `ip_bin` ) FROM `log_metadata`
gives me
4333D26E000000000000000000000000
And
SELECT INET_NTOA(0x4333D26E)
gives me 67.51.210.110
So I tried:
SELECT
SUBSTRING( CONVERT(HEX(`ip_bin`), CHAR(32)), 1, 8 ) AS `A`
, INET_NTOA(
SUBSTRING( CONVERT(HEX(`ip_bin`), CHAR(32)), 1, 8 )
) AS `B`
, INET_NTOA(hex(`ip_bin`)) AS `C`
, INET_NTOA(`ip_bin`) AS `D`
FROM `log_metadata`
But I only get
+----------+------------+------------+---------+
| A | B | C | D |
+----------+------------+------------+---------+
| 4333D26E | 0.0.16.237 | 0.0.16.237 | 0.0.0.0 |
+----------+------------+------------+---------+
Any suggestions?
mysql string binary string-formatting
I'm trying to convert A binary IP to a human-readable IP
SELECT HEX( `ip_bin` ) FROM `log_metadata`
gives me
4333D26E000000000000000000000000
And
SELECT INET_NTOA(0x4333D26E)
gives me 67.51.210.110
So I tried:
SELECT
SUBSTRING( CONVERT(HEX(`ip_bin`), CHAR(32)), 1, 8 ) AS `A`
, INET_NTOA(
SUBSTRING( CONVERT(HEX(`ip_bin`), CHAR(32)), 1, 8 )
) AS `B`
, INET_NTOA(hex(`ip_bin`)) AS `C`
, INET_NTOA(`ip_bin`) AS `D`
FROM `log_metadata`
But I only get
+----------+------------+------------+---------+
| A | B | C | D |
+----------+------------+------------+---------+
| 4333D26E | 0.0.16.237 | 0.0.16.237 | 0.0.0.0 |
+----------+------------+------------+---------+
Any suggestions?
mysql string binary string-formatting
mysql string binary string-formatting
asked Mar 12 '13 at 19:47
redolentredolent
2,80432537
2,80432537
Probably is the missing0x
in front of the stringA
.
– Niloct
Mar 12 '13 at 19:51
Tried that. It just returned0
and0.0.0.0
– redolent
Mar 12 '13 at 19:54
add a comment |
Probably is the missing0x
in front of the stringA
.
– Niloct
Mar 12 '13 at 19:51
Tried that. It just returned0
and0.0.0.0
– redolent
Mar 12 '13 at 19:54
Probably is the missing
0x
in front of the string A
.– Niloct
Mar 12 '13 at 19:51
Probably is the missing
0x
in front of the string A
.– Niloct
Mar 12 '13 at 19:51
Tried that. It just returned
0
and 0.0.0.0
– redolent
Mar 12 '13 at 19:54
Tried that. It just returned
0
and 0.0.0.0
– redolent
Mar 12 '13 at 19:54
add a comment |
4 Answers
4
active
oldest
votes
mysql> select inet_ntoa(conv('4333d26e', 16, 10));
+-------------------------------------+
| inet_ntoa(conv('4333d26e', 16, 10)) |
+-------------------------------------+
| 67.51.210.110 |
+-------------------------------------+
1 row in set (0.00 sec)
Check if it works there too =)
Edit
The problem is that inet_ntoa
seems to parse from decimal strings
number representation, not hexadecimal ones, or from hexadecimal integers
. Compare:
mysql> select inet_ntoa(0x4333d26e);
+-----------------------+
| inet_ntoa(0x4333d26e) |
+-----------------------+
| 67.51.210.110 |
+-----------------------+
1 row in set (0.02 sec)
mysql> select inet_ntoa('0x4333d26e');
+-------------------------+
| inet_ntoa('0x4333d26e') |
+-------------------------+
| 0.0.0.0 |
+-------------------------+
1 row in set, 1 warning (0.00 sec)
Edit
This is simpler and seems to work too:
SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata
1
I ended up usingINET_NTOA( CONV( SUBSTRING(HEX( ip_bin ), 1, 8), 16, 10 ))
– redolent
Mar 12 '13 at 20:09
Try this:SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata
– Niloct
Mar 12 '13 at 20:24
Didn't work. The problem is that the bits are on the left for some reason -0x4333D26E000000000000000000000000
– redolent
Mar 12 '13 at 23:13
You can try shifting those to the right. Mysql has shift operators.
– Niloct
Mar 13 '13 at 0:42
MySQL seems to hate those, however. My best guess is it parses the value to be integer max
– redolent
Mar 14 '13 at 20:51
|
show 2 more comments
I found I had to call HEX
to convert my binary field to a hex string first, so the following worked for me:
select inet_ntoa(conv(HEX(ip_bin), 16, 10)) from log_metadata
add a comment |
When using Mysql 5.6.3 or later, it is easier to just use INET6_NTOA
- it takes a binary string and returns the human readable format for it. It also supports both IPv4 and IPv6 addresses and returns the format accordingly. So in your example you would use:
SELECT INET6_NTOA( `ip_bin` ) FROM `log_metadata`
And should get the human readable result.
add a comment |
FYI~ this works in newer version of mysql
SELECT INET6_NTOA( `ip_bin` ) FROM `log_metadata`
which is the same as
SELECT INET_NTOA( CONV( SUBSTRING(HEX(`ip_bin` ), 1, 8), 16, 10 )) FROM log_metadata;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15370721%2fsql-binary-to-ip-address%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
mysql> select inet_ntoa(conv('4333d26e', 16, 10));
+-------------------------------------+
| inet_ntoa(conv('4333d26e', 16, 10)) |
+-------------------------------------+
| 67.51.210.110 |
+-------------------------------------+
1 row in set (0.00 sec)
Check if it works there too =)
Edit
The problem is that inet_ntoa
seems to parse from decimal strings
number representation, not hexadecimal ones, or from hexadecimal integers
. Compare:
mysql> select inet_ntoa(0x4333d26e);
+-----------------------+
| inet_ntoa(0x4333d26e) |
+-----------------------+
| 67.51.210.110 |
+-----------------------+
1 row in set (0.02 sec)
mysql> select inet_ntoa('0x4333d26e');
+-------------------------+
| inet_ntoa('0x4333d26e') |
+-------------------------+
| 0.0.0.0 |
+-------------------------+
1 row in set, 1 warning (0.00 sec)
Edit
This is simpler and seems to work too:
SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata
1
I ended up usingINET_NTOA( CONV( SUBSTRING(HEX( ip_bin ), 1, 8), 16, 10 ))
– redolent
Mar 12 '13 at 20:09
Try this:SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata
– Niloct
Mar 12 '13 at 20:24
Didn't work. The problem is that the bits are on the left for some reason -0x4333D26E000000000000000000000000
– redolent
Mar 12 '13 at 23:13
You can try shifting those to the right. Mysql has shift operators.
– Niloct
Mar 13 '13 at 0:42
MySQL seems to hate those, however. My best guess is it parses the value to be integer max
– redolent
Mar 14 '13 at 20:51
|
show 2 more comments
mysql> select inet_ntoa(conv('4333d26e', 16, 10));
+-------------------------------------+
| inet_ntoa(conv('4333d26e', 16, 10)) |
+-------------------------------------+
| 67.51.210.110 |
+-------------------------------------+
1 row in set (0.00 sec)
Check if it works there too =)
Edit
The problem is that inet_ntoa
seems to parse from decimal strings
number representation, not hexadecimal ones, or from hexadecimal integers
. Compare:
mysql> select inet_ntoa(0x4333d26e);
+-----------------------+
| inet_ntoa(0x4333d26e) |
+-----------------------+
| 67.51.210.110 |
+-----------------------+
1 row in set (0.02 sec)
mysql> select inet_ntoa('0x4333d26e');
+-------------------------+
| inet_ntoa('0x4333d26e') |
+-------------------------+
| 0.0.0.0 |
+-------------------------+
1 row in set, 1 warning (0.00 sec)
Edit
This is simpler and seems to work too:
SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata
1
I ended up usingINET_NTOA( CONV( SUBSTRING(HEX( ip_bin ), 1, 8), 16, 10 ))
– redolent
Mar 12 '13 at 20:09
Try this:SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata
– Niloct
Mar 12 '13 at 20:24
Didn't work. The problem is that the bits are on the left for some reason -0x4333D26E000000000000000000000000
– redolent
Mar 12 '13 at 23:13
You can try shifting those to the right. Mysql has shift operators.
– Niloct
Mar 13 '13 at 0:42
MySQL seems to hate those, however. My best guess is it parses the value to be integer max
– redolent
Mar 14 '13 at 20:51
|
show 2 more comments
mysql> select inet_ntoa(conv('4333d26e', 16, 10));
+-------------------------------------+
| inet_ntoa(conv('4333d26e', 16, 10)) |
+-------------------------------------+
| 67.51.210.110 |
+-------------------------------------+
1 row in set (0.00 sec)
Check if it works there too =)
Edit
The problem is that inet_ntoa
seems to parse from decimal strings
number representation, not hexadecimal ones, or from hexadecimal integers
. Compare:
mysql> select inet_ntoa(0x4333d26e);
+-----------------------+
| inet_ntoa(0x4333d26e) |
+-----------------------+
| 67.51.210.110 |
+-----------------------+
1 row in set (0.02 sec)
mysql> select inet_ntoa('0x4333d26e');
+-------------------------+
| inet_ntoa('0x4333d26e') |
+-------------------------+
| 0.0.0.0 |
+-------------------------+
1 row in set, 1 warning (0.00 sec)
Edit
This is simpler and seems to work too:
SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata
mysql> select inet_ntoa(conv('4333d26e', 16, 10));
+-------------------------------------+
| inet_ntoa(conv('4333d26e', 16, 10)) |
+-------------------------------------+
| 67.51.210.110 |
+-------------------------------------+
1 row in set (0.00 sec)
Check if it works there too =)
Edit
The problem is that inet_ntoa
seems to parse from decimal strings
number representation, not hexadecimal ones, or from hexadecimal integers
. Compare:
mysql> select inet_ntoa(0x4333d26e);
+-----------------------+
| inet_ntoa(0x4333d26e) |
+-----------------------+
| 67.51.210.110 |
+-----------------------+
1 row in set (0.02 sec)
mysql> select inet_ntoa('0x4333d26e');
+-------------------------+
| inet_ntoa('0x4333d26e') |
+-------------------------+
| 0.0.0.0 |
+-------------------------+
1 row in set, 1 warning (0.00 sec)
Edit
This is simpler and seems to work too:
SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata
edited Mar 12 '13 at 20:30
answered Mar 12 '13 at 19:59
NiloctNiloct
5,39432745
5,39432745
1
I ended up usingINET_NTOA( CONV( SUBSTRING(HEX( ip_bin ), 1, 8), 16, 10 ))
– redolent
Mar 12 '13 at 20:09
Try this:SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata
– Niloct
Mar 12 '13 at 20:24
Didn't work. The problem is that the bits are on the left for some reason -0x4333D26E000000000000000000000000
– redolent
Mar 12 '13 at 23:13
You can try shifting those to the right. Mysql has shift operators.
– Niloct
Mar 13 '13 at 0:42
MySQL seems to hate those, however. My best guess is it parses the value to be integer max
– redolent
Mar 14 '13 at 20:51
|
show 2 more comments
1
I ended up usingINET_NTOA( CONV( SUBSTRING(HEX( ip_bin ), 1, 8), 16, 10 ))
– redolent
Mar 12 '13 at 20:09
Try this:SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata
– Niloct
Mar 12 '13 at 20:24
Didn't work. The problem is that the bits are on the left for some reason -0x4333D26E000000000000000000000000
– redolent
Mar 12 '13 at 23:13
You can try shifting those to the right. Mysql has shift operators.
– Niloct
Mar 13 '13 at 0:42
MySQL seems to hate those, however. My best guess is it parses the value to be integer max
– redolent
Mar 14 '13 at 20:51
1
1
I ended up using
INET_NTOA( CONV( SUBSTRING(HEX( ip_bin ), 1, 8), 16, 10 ))
– redolent
Mar 12 '13 at 20:09
I ended up using
INET_NTOA( CONV( SUBSTRING(HEX( ip_bin ), 1, 8), 16, 10 ))
– redolent
Mar 12 '13 at 20:09
Try this:
SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata
– Niloct
Mar 12 '13 at 20:24
Try this:
SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata
– Niloct
Mar 12 '13 at 20:24
Didn't work. The problem is that the bits are on the left for some reason -
0x4333D26E000000000000000000000000
– redolent
Mar 12 '13 at 23:13
Didn't work. The problem is that the bits are on the left for some reason -
0x4333D26E000000000000000000000000
– redolent
Mar 12 '13 at 23:13
You can try shifting those to the right. Mysql has shift operators.
– Niloct
Mar 13 '13 at 0:42
You can try shifting those to the right. Mysql has shift operators.
– Niloct
Mar 13 '13 at 0:42
MySQL seems to hate those, however. My best guess is it parses the value to be integer max
– redolent
Mar 14 '13 at 20:51
MySQL seems to hate those, however. My best guess is it parses the value to be integer max
– redolent
Mar 14 '13 at 20:51
|
show 2 more comments
I found I had to call HEX
to convert my binary field to a hex string first, so the following worked for me:
select inet_ntoa(conv(HEX(ip_bin), 16, 10)) from log_metadata
add a comment |
I found I had to call HEX
to convert my binary field to a hex string first, so the following worked for me:
select inet_ntoa(conv(HEX(ip_bin), 16, 10)) from log_metadata
add a comment |
I found I had to call HEX
to convert my binary field to a hex string first, so the following worked for me:
select inet_ntoa(conv(HEX(ip_bin), 16, 10)) from log_metadata
I found I had to call HEX
to convert my binary field to a hex string first, so the following worked for me:
select inet_ntoa(conv(HEX(ip_bin), 16, 10)) from log_metadata
answered Dec 15 '14 at 16:49
Brad ParksBrad Parks
29.5k36165225
29.5k36165225
add a comment |
add a comment |
When using Mysql 5.6.3 or later, it is easier to just use INET6_NTOA
- it takes a binary string and returns the human readable format for it. It also supports both IPv4 and IPv6 addresses and returns the format accordingly. So in your example you would use:
SELECT INET6_NTOA( `ip_bin` ) FROM `log_metadata`
And should get the human readable result.
add a comment |
When using Mysql 5.6.3 or later, it is easier to just use INET6_NTOA
- it takes a binary string and returns the human readable format for it. It also supports both IPv4 and IPv6 addresses and returns the format accordingly. So in your example you would use:
SELECT INET6_NTOA( `ip_bin` ) FROM `log_metadata`
And should get the human readable result.
add a comment |
When using Mysql 5.6.3 or later, it is easier to just use INET6_NTOA
- it takes a binary string and returns the human readable format for it. It also supports both IPv4 and IPv6 addresses and returns the format accordingly. So in your example you would use:
SELECT INET6_NTOA( `ip_bin` ) FROM `log_metadata`
And should get the human readable result.
When using Mysql 5.6.3 or later, it is easier to just use INET6_NTOA
- it takes a binary string and returns the human readable format for it. It also supports both IPv4 and IPv6 addresses and returns the format accordingly. So in your example you would use:
SELECT INET6_NTOA( `ip_bin` ) FROM `log_metadata`
And should get the human readable result.
answered Oct 18 '17 at 15:26
iquitoiquito
1,353717
1,353717
add a comment |
add a comment |
FYI~ this works in newer version of mysql
SELECT INET6_NTOA( `ip_bin` ) FROM `log_metadata`
which is the same as
SELECT INET_NTOA( CONV( SUBSTRING(HEX(`ip_bin` ), 1, 8), 16, 10 )) FROM log_metadata;
add a comment |
FYI~ this works in newer version of mysql
SELECT INET6_NTOA( `ip_bin` ) FROM `log_metadata`
which is the same as
SELECT INET_NTOA( CONV( SUBSTRING(HEX(`ip_bin` ), 1, 8), 16, 10 )) FROM log_metadata;
add a comment |
FYI~ this works in newer version of mysql
SELECT INET6_NTOA( `ip_bin` ) FROM `log_metadata`
which is the same as
SELECT INET_NTOA( CONV( SUBSTRING(HEX(`ip_bin` ), 1, 8), 16, 10 )) FROM log_metadata;
FYI~ this works in newer version of mysql
SELECT INET6_NTOA( `ip_bin` ) FROM `log_metadata`
which is the same as
SELECT INET_NTOA( CONV( SUBSTRING(HEX(`ip_bin` ), 1, 8), 16, 10 )) FROM log_metadata;
answered Nov 14 '18 at 14:59
Mr HeelisMr Heelis
1,07111221
1,07111221
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15370721%2fsql-binary-to-ip-address%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Probably is the missing
0x
in front of the stringA
.– Niloct
Mar 12 '13 at 19:51
Tried that. It just returned
0
and0.0.0.0
– redolent
Mar 12 '13 at 19:54