N
N
NeOn4eG2011-07-18 09:36:00
PHP
NeOn4eG, 2011-07-18 09:36:00

Decode field value in hex format in MySQL

Good morning.
Mysql has an ip field, type char(8), encoding cp1251_bin. IP addresses are stored in this format - d4e82335 .
Question - how to decode an ip address into a normal form in php or directly in a mysql query?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey, 2011-07-18
@NeOn4eG

SELECT INET_NTOA(CONV(ip, 16, 10));

N
Napseg, 2011-07-18
@Napseg

in php

$hex='d4e82335';
for($i=0;$i<7;$i=$i+2){
$dec[$i]=hexdec(substr($hex,$i,2));		
}
$dec="$dec[0].$dec[2].$dec[4].$dec[6]";
echo($dec);

S
Sergey Beresnev, 2011-07-18
@sectus

I see that a solution has been found for MySQL, but I will still offer it in PHP, otherwise terrible bicycles have appeared here.

$str = 'd4e82335';
echo long2ip(hexdec($str)); //212.232.35.53

I
interrupt_controller, 2011-07-18
@interrupt_controller

>Maybe this is the hex spelling of the IP address 212.232.35.53
if so, then here is the function prototype in php:
int hexdec(string hex_string)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question