问题描述
我正在尝试使用presto将十六进制字符串(以"0x"开头)转换为整数值.例如,0x100
到256.我的十六进制字符串称为msg_id.我尝试使用此-
I'm trying to convert hex string (starts with '0x') to it's integer value using presto. For example 0x100
to 256. My hex string is called msg_id.I tried to use this-
from_hex(substr(msg_id,3))
但是我遇到了一个问题,因为from_hex
期望偶数个十六进制数字(0100
而不是100
).我决定尝试使用if
语句解决此问题,因此我尝试了以下操作:
But I run into a problem, because from_hex
expect even number of hex digits (0100
instead of 100
).I decided to try and solve this using an if
statement, so I tried this:
if(length(msg_id)%2=0, from_hex(substr(msg_id,3)))
(稍后将处理奇数个数字)
(will take care of the odd number of digits case later)
但是-from_hex
的结果是varbinary
类型,具有不同数量的字节.我想将其转换为Integer或任何其他数字类型,但是我找不到解决方法.
But- the result of from_hex
is a varbinary
type, with a varied number of bytes. I want to convert it to an Integer, or any other numeric type, but I can't find a way to do it.
有什么想法吗?我将不胜感激...
Any ideas? I would appreciate it...
推荐答案
您可以使用 from_base(string, radix)
将用十六进制数字写的数字解析为bigint
.您只需要先去除前导'0x'
:
You can use from_base(string, radix)
to parse number written with hex digits into a bigint
. You just need to strip leading '0x'
first:
select from_base(substr('0x100', 3), 16);
_col0
-------
256
或 regexp_replace()
:
presto:tiny> select from_base(regexp_replace('0x100', '^0x'), 16);
_col0
-------
256
这篇关于Presto-将十六进制字符串转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!