Skip to content

Latest commit

 

History

History
124 lines (82 loc) · 2.89 KB

20201205_02.md

File metadata and controls

124 lines (82 loc) · 2.89 KB

PostgreSQL 十进制、十六进制、二进制、八进制转换

作者

digoal

日期

2020-12-05

标签

PostgreSQL , 十进制 , 十六进制 , 八进制, 二进制


背景

https://circuitspedia.com/number-system-decimal-binary-hexa-octal-conversion/

https://www.cnblogs.com/ode/p/postgresql_to_hex_decimal.html

十进制转十六进制和二进制

mydb=# SELECT to_hex(10);  
 to_hex   
--------  
 a  
(1 row)  
  
  
mydb=# SELECT 10::bit(4);  
 bit    
------  
 1010  
(1 row)  

注意截断

postgres=> SELECT 10::bit(40);  
                   bit                      
------------------------------------------  
 0000000000000000000000000000000000001010  
(1 row)  
  
postgres=> SELECT 10::bit(1);  
 bit   
-----  
 0  
(1 row)  
  
postgres=> SELECT 10::bit(3);  
 bit   
-----  
 010  
(1 row)  

十六进制转十进制和二进制

mydb=# SELECT x'A'::int;  
 int4   
------  
   10  
(1 row)  
  
   
mydb=# SELECT x'A'::bit(4);  
 bit    
------  
 1010  
(1 row)  
  
postgres=> select x'bcd'::varbit;  
    varbit      
--------------  
 101111001101  
(1 row)  

二进制转十进制和十六进制

mydb=# SELECT B'1010'::int;  
 int4   
------  
   10  
(1 row)  
  
  
mydb=# SELECT to_hex(B'1010'::int);  
 to_hex   
--------  
 a  
(1 row)  

您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。开不开森.

digoal's wechat