在PostgreSQL中,二进制、十进制、十六进制之间的转换是非常方便的,如下:
十进制转十六进制和二进制
mydb=# SELECT to_hex(10);
to_hex
--------
a
(1 row)
mydb=# SELECT 10::bit(4);
bit
------
1010
(1 row)
十六进制转十进制和二进制
mydb=# SELECT x\'A\'::int;
int4
------
10
(1 row)
mydb=# SELECT x\'A\'::bit(4);
bit
------
1010
(1 row)
二进制转十进制和十六进制
mydb=# SELECT B\'1010\'::int;
int4
------
10
(1 row)
mydb=# SELECT to_hex(B\'1010\'::int);
to_hex
--------
a
(1 row)