Presto-将十六进制字符串转换为int [英] Presto - hex string to int

查看:1395
本文介绍了Presto-将十六进制字符串转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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...

推荐答案

您可以使用

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆