我们在开发过程中需要在数据库中编写函数,完成CRC校验计算,但是发现了一个问题,请帮忙解决一下!
java程序
public static int UpdateCRC(int crc, final int c) {
return ((crc >> 8) ^ c) & 0xFF;
}
crc=0 c=-86
返回值为170
postgresql
declare
crc int;
c int;
BEGIN
crc:= 0;
c:=-86;
DBMS_OUTPUT.PUT_LINE(((crc >> 8)^c) & (255::double));
end;
执行报错,请您看看哪里出了问题?
你的例子中用到了>>,^,&操作符,这些操作符对应的操作数类型如下:
postgres=# \do+ >>
List of operators
Schema | Name | Left arg type | Right arg type | Result type | Function | Description
------------+------+---------------+----------------+-------------+---------------+---------------------
pg_catalog | >> | anyrange | anyrange | boolean | range_after | is right of
pg_catalog | >> | bigint | integer | bigint | int8shr | bitwise shift right
pg_catalog | >> | bit | integer | bit | bitshiftright | bitwise shift right
pg_catalog | >> | box | box | boolean | box_right | is right of
pg_catalog | >> | circle | circle | boolean | circle_right | is right of
pg_catalog | >> | inet | inet | boolean | network_sup | is supernet
pg_catalog | >> | integer | integer | integer | int4shr | bitwise shift right
pg_catalog | >> | point | point | boolean | point_right | is right of
pg_catalog | >> | polygon | polygon | boolean | poly_right | is right of
pg_catalog | >> | smallint | integer | smallint | int2shr | bitwise shift right
(10 rows)
postgres=# \do+ &
List of operators
Schema | Name | Left arg type | Right arg type | Result type | Function | Description
------------+------+---------------+----------------+-------------+-------------+-------------
pg_catalog | & | bigint | bigint | bigint | int8and | bitwise and
pg_catalog | & | bit | bit | bit | bitand | bitwise and
pg_catalog | & | inet | inet | inet | inetand | bitwise and
pg_catalog | & | integer | integer | integer | int4and | bitwise and
pg_catalog | & | macaddr | macaddr | macaddr | macaddr_and | bitwise and
pg_catalog | & | smallint | smallint | smallint | int2and | bitwise and
(6 rows)
postgres=# \do+ ^
List of operators
Schema | Name | Left arg type | Right arg type | Result type | Function | Description
------------+------+------------------+------------------+------------------+---------------+----------------
pg_catalog | ^ | double precision | double precision | double precision | dpow | exponentiation
pg_catalog | ^ | numeric | numeric | numeric | numeric_power | exponentiation
(2 rows)
postgres=# select ((2)^(-86)) & 0xFF;
ERROR: operator does not exist: double precision & integer
LINE 1: select ((2)^(-86)) & 0xFF;
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
报错的是&这个操作符,因为^产生了numeric的结果,所以需要转成&支持的类型才行。
解决办法,转换成int8:
postgres=# select ((2)^(-86))::int8 & 0xFF;
xff
-----
0
(1 row)
如果要实现JAVA程序一样的效果,需要修改一下写法,^在PG里是pow,不是亦或。亦或是#符号。
postgres=# select ((0) # (-86))::int4 & 255;
?column?
----------
170
(1 row)
今天在遇到同类问题: date类型数据 减去 (默认精度)numeric数据类型。一直提示:Error:operator does not exists :date - numeric。
显示转换 ::integer,解决。 多谢。
-------------------------
把错误日志也贴出来
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。