Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在Postgres中将十六进制字符串转换为bigint_Sql_Postgresql_Hex_Postgresql 9.1 - Fatal编程技术网

Sql 在Postgres中将十六进制字符串转换为bigint

Sql 在Postgres中将十六进制字符串转换为bigint,sql,postgresql,hex,postgresql-9.1,Sql,Postgresql,Hex,Postgresql 9.1,我想将HTML使用的十六进制字符串转换为bigint,然后通过PL/pgSQL编写的函数在Postgres中将其转换为单独的R、G和B值 我可以将字符串解码为bytea,如下所示: hex bytea := decode(hex, 'hex'); 在具有固定值的查询中,这就像一个美人: select ( array[ (cast(x'ffaa33' as bigint) >> 16) % 256, (cast(x'ffaa33' as bigint)

我想将HTML使用的十六进制字符串转换为
bigint
,然后通过PL/pgSQL编写的函数在Postgres中将其转换为单独的R、G和B值

我可以将字符串解码为
bytea
,如下所示:

hex bytea := decode(hex, 'hex');
在具有固定值的查询中,这就像一个美人:

select ( array[ (cast(x'ffaa33' as bigint) >> 16) % 256,
                (cast(x'ffaa33' as bigint) >> 8) % 256,
                 cast(x'ffaa33' as bigint) % 256 ] )
但我无法将这两个参数组合在一起,例如将'ffaa33'作为参数传递

有人有更好的主意吗?我正在使用PosgreSQL 9.1?

一个简单的方法是:

 select ('x'||lpad(the_hex_value,16,'0'))::bit(64)::bigint;
因为最左边的位总是被解释为符号位,所以必须用0填充左边。
还要记住,
bigint
是有符号的,postgres没有内置的无符号类型。

谢谢Daniel。如何将bigint转换为hex?@Cherven:postgres有一个内置的
转换为hex(bigint)
功能