有人能做得更好吗?
sqlite> .schema
CREATE TABLE IF NOT EXISTS "hex2val" (
"hex" TEXT NOT NULL,
"val" INTEGER NOT NULL,
PRIMARY KEY("hex")
);
CREATE TABLE power (x int primary key not null, power not null);
sqlite> select * from hex2val;
0|0
1|1
2|2
3|3
4|4
5|5
6|6
7|7
8|8
9|9
A|10
B|11
C|12
D|13
E|14
F|15
sqlite> select * from power;
1|1
2|16
3|256
4|4096
5|65536
执行中:
WITH str as (
select 1 pos, '0' value, 1 len UNION ALL
select 2 pos, '21F' value, 3 len UNION ALL
select 3 pos, '3B51' value, 4 len UNION ALL
select 4 pos, 'FFFF' value, 4 len
),
cnt as (WITH recursive cnt(idx) AS (SELECT 1 UNION ALL SELECT idx+1 FROM cnt LIMIT (select max(len) maxlen from str)) select * from cnt)
select pos, value, sum(val * power)
FROM str, cnt, hex2val, power
where idx <= len
and hex = substr(value, idx, 1)
and x = len -idx + 1
group by pos
结果:
1 0 0
2 21F 543
3 3B51 15185
4 FFFF 65535
我想删除'str'的冗余-它应仅容纳字符串。
而且我想摆脱掉缺少的转换函数和pow()函数的辅助表'hex2val'和'power'。