Home  >  Article  >  Database  >  How to convert data to hexadecimal in mysql

How to convert data to hexadecimal in mysql

青灯夜游
青灯夜游Original
2022-01-06 11:46:537720browse

How to convert mysql to hexadecimal: 1. Use the hex() function, the syntax "select hex (decimal value);", to convert decimal to hexadecimal; 2. Use the conv() function , the syntax "select conv('data value', original base, 16);" can convert any base to hexadecimal.

How to convert data to hexadecimal in mysql

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

mysql converts data to hexadecimal

##Method 1: Use hex() function

hex() function can convert decimal to hexadecimal and return hexadecimal string representation.

Syntax:


HEX(N_or_S)

If N_or_S is a number, returns the string representation of the hexadecimal value of N, where N is a long long (BIGINT) number. This is equivalent to CONV(N,10,16). If N_or_S is a string, returns the hexadecimal string representation of N_or_S, where each character in N_or_S is converted to two hexadecimal digits.

Example:


mysql> SELECT HEX(255);
+---------------------------------------------------------+
|                        HEX(255)                         |
+---------------------------------------------------------+
|                           FF                            |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT 0x616263;
+---------------------------------------------------------+
|                        0x616263                         |
+---------------------------------------------------------+
|                          abc                            |
+---------------------------------------------------------+
1 row in set (0.00 sec)

Method 2: Use conv() function

CONV - hexadecimal conversion.

CONV(N,from_base,to_base)

The purpose of the function is to convert numbers between different number bases. This function returns a string of N values ​​converted from from_base to to_base. The minimum base value is 2 and the maximum base value is 36. If any argument is NULL, the function returns NULL. Consider the following example that converts the number 5 from base 16 to base 2

Example:

decimal to hexadecimal

select conv('20',10,16);

How to convert data to hexadecimal in mysql

Convert binary to decimal

select conv('101',2,10);

How to convert data to hexadecimal in mysql

[Related recommendations:

mysql video tutorial]

The above is the detailed content of How to convert data to hexadecimal in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn