将一串 HEX 转换为多个整数
Convert a string of HEX to multiple integers
我有一个十六进制字符串列表,需要将其转换为 3 个不同的整数(int32、int32、int16)
例如:
0x6d,0xb,0x0,0x0,0x6e,0x3,0x0,0x0,0x4e,0x0
将转换为:
First 4 = 0xb6d = 2925
Next 4 = 0x36e = 878
Last 2 = 0x4e = 78
如果可能,每一个都应该是一个单独的列。
例如:
2925 | 878| 78
知道如何在 sql Google Bigquery 中执行此操作吗?
谢谢
考虑以下方法
select * from (
select col, div(offset, 4) grp, cast('0x' || string_agg(replace(el, '0x', ''), '' order by mod(offset, 4) desc) as int64) val
from your_table, unnest(split(col)) el with offset
group by col, grp
)
pivot (any_value(val) as val for grp in (0, 1, 2))
如果应用于您问题中的样本数据,如
with your_table as (
select '0x6d,0xb,0x0,0x0,0x6e,0x3,0x0,0x0,0x4e,0x0' col
)
输出是
我有一个十六进制字符串列表,需要将其转换为 3 个不同的整数(int32、int32、int16)
例如:
0x6d,0xb,0x0,0x0,0x6e,0x3,0x0,0x0,0x4e,0x0
将转换为:
First 4 = 0xb6d = 2925
Next 4 = 0x36e = 878
Last 2 = 0x4e = 78
如果可能,每一个都应该是一个单独的列。 例如:
2925 | 878| 78
知道如何在 sql Google Bigquery 中执行此操作吗? 谢谢
考虑以下方法
select * from (
select col, div(offset, 4) grp, cast('0x' || string_agg(replace(el, '0x', ''), '' order by mod(offset, 4) desc) as int64) val
from your_table, unnest(split(col)) el with offset
group by col, grp
)
pivot (any_value(val) as val for grp in (0, 1, 2))
如果应用于您问题中的样本数据,如
with your_table as (
select '0x6d,0xb,0x0,0x0,0x6e,0x3,0x0,0x0,0x4e,0x0' col
)
输出是