gpt4 book ai didi

mysql - SQL 在不同的字节中拆分 hexastring

转载 作者:行者123 更新时间:2023-11-28 23:08:09 25 4
gpt4 key购买 nike

我有一个 google bigquery 看起来像这样:

    #standardSQL
SELECT
timestamp,
CAN_Frame,
TRIM(SPLIT(CAN_Frame)[OFFSET(4)]) AS bytes
FROM
`data.source`
WHERE
LENGTH(CAN_Frame) > 1 and
SUBSTR(TRIM(SPLIT(CAN_Frame)[OFFSET(4)]),1,2) IN ('83', '84')
ORDER BY
timestamp DESC
LIMIT
8000

或者像这样

#standardSQL
SELECT
*
FROM (
SELECT
timestamp,
CAN_Frame,
REGEXP_EXTRACT(CAN_Frame, r', ([^,]+)$') AS bytes_string,
FROM_HEX(REPLACE(REGEXP_EXTRACT(CAN_Frame, r', ([^,]+)$'), ' ', '')) AS
bytes

FROM `data.source`
)
WHERE SUBSTR(bytes, 1, 1) IN (b'\x83', b'\x84')

ORDER BY timestamp DESC
LIMIT 8000

结果表:

行时间戳 CAN_Frame 字节
1 2017-09-29 14:31:02 UTC S,48778,410,8, 84 10 00 25 00 21 00 4F 84 10 0025 00 21 00 4F
2 2017-09-29 14:30:42 UTC S,35847,480,8, 83 80 00 01 00 03 00 0D 83 80 0001 00 03 00 0D
3 2017-09-29 14:30:40 UTC S,34612,4B2,8, 84 B2 00 27 00 08 00 03 84 B2 0027 00 08 00 03

行时间戳 CAN_Frame bytes_string bytes
1 2017-09-29 14:31:02 UTC S,48778,410,8, 84 10 00 25 00 21 00 4F 84 10 00 25 00 21 00 4F hBAAJQAhAE8=
2 2017-09-29 14:30:42 UTC S,35847,480,8, 83 80 00 01 00 03 00 0D 83 80 00 01 00 03 00 0D g4AAAQADAA0=
3 2017-09-29 14:30:40 UTC S,34612,4B2,8, 84 B2 00 27 00 08 00 03 84 B2 00 27 00 08 00 03 hLIAJwAIAAM=
4 2017-09-29 14:30:39 UTC S,34314,4C0,8, 84 C0 00 1C 00 15 00 07 84 C0 00 1C 00 15 00 07 hMAAHAAVAAc=

我的问题和问题现在是如何拆分 8 字节六角字符串,让我得到字符串的第 6 和第 7 个字节以 83 开头,第 8 个字节从 83 开始,第 3 个字节从 84 开始,第 4 个和第 4 个字节开始以 84 开头的字符串的第 5 个字节。这些数据对是我需要读取的 unsigned int 中具有 lsb msb 的值。

我希望有人能帮助我或至少理解我的问题。

最好的问候

最佳答案

#standardSQL
WITH `data.source` AS (
SELECT 'S,0,2B3,8, C2 B3 00 00 00 00 03 DE' AS frame UNION ALL
SELECT 'S,0,3FA,6, 00 E0 04 A5 00 0B' UNION ALL
SELECT 'S,0,440,8, 83 40 4E A5 00 47 00 64' UNION ALL
SELECT 'S,0,450,8, 84 50 01 12 01 19 01 B3' UNION ALL
SELECT 'S,0,4B0,8, 84 B0 4E A5 00 43 00 64'
)
SELECT
frame, bytes, STRING_AGG(b, ' ' ORDER BY p) AS selected_bytes
FROM (
SELECT frame, TRIM(SPLIT(frame)[OFFSET(4)]) AS bytes, SUBSTR(TRIM(SPLIT(frame)[OFFSET(4)]), 1, 2) AS f
FROM `data.source`
WHERE SUBSTR(TRIM(SPLIT(frame)[OFFSET(4)]), 1, 2) IN ('83', '84')
), UNNEST(SPLIT(bytes, ' ')) AS b WITH OFFSET AS p
WHERE CASE f WHEN '83' THEN p IN (5, 6, 7) WHEN '84' THEN p IN (2, 3, 4) END
GROUP BY frame, bytes
-- ORDER BY frame

结果是:

frame                               bytes                       selected_bytes
S,0,440,8, 83 40 4E A5 00 47 00 64 83 40 4E A5 00 47 00 64 47 00 64
S,0,450,8, 84 50 01 12 01 19 01 B3 84 50 01 12 01 19 01 B3 01 12 01
S,0,4B0,8, 84 B0 4E A5 00 43 00 64 84 B0 4E A5 00 43 00 64 4E A5 00

更新:

byte 6 and 7 from the string beginning with 83 called Aiout
one column contains byte 8 from the string beginning with 83 and byte 3 from the string beginning with 84 called Biout
one column contains byte 4 and 5 from the string beginning with 84 called Avout
Bvout containing byte 6 and 7 from the string beginning with 84

#standardSQL
WITH `data.source` AS (
SELECT 'S,0,2B3,8, C2 B3 00 00 00 00 03 DE' AS frame UNION ALL
SELECT 'S,0,3FA,6, 00 E0 04 A5 00 0B' UNION ALL
SELECT 'S,0,440,8, 83 40 4E A5 00 47 00 64' UNION ALL
SELECT 'S,0,450,8, 84 50 01 12 01 19 01 B3' UNION ALL
SELECT 'S,0,4B0,8, 84 B0 4E A5 00 43 00 64'
)
SELECT
frame, bytes,
STRING_AGG(CASE WHEN f='83' AND p IN (5, 6) THEN b ELSE '' END, ' ' ORDER BY p) AS Aiout,
STRING_AGG(CASE WHEN (f='83' AND p=7) OR (f='84' AND p=2) THEN b ELSE '' END, ' ' ORDER BY p) AS Biout,
STRING_AGG(CASE WHEN f='84' AND p IN (3, 4) THEN b ELSE '' END, ' ' ORDER BY p) AS Avout,
STRING_AGG(CASE WHEN f='84' AND p IN (5, 6) THEN b ELSE '' END, ' ' ORDER BY p) AS Bvout
FROM (
SELECT frame, TRIM(SPLIT(frame)[OFFSET(4)]) AS bytes, SUBSTR(TRIM(SPLIT(frame)[OFFSET(4)]), 1, 2) AS f
FROM `data.source`
WHERE SUBSTR(TRIM(SPLIT(frame)[OFFSET(4)]), 1, 2) IN ('83', '84')
), UNNEST(SPLIT(bytes, ' ')) AS b WITH OFFSET AS p
GROUP BY frame, bytes
ORDER BY frame

输出为

frame                               bytes                   Aiout   Biout   Avout   Bvout
S,0,440,8, 83 40 4E A5 00 47 00 64 83 40 4E A5 00 47 00 64 47 00 64
S,0,450,8, 84 50 01 12 01 19 01 B3 84 50 01 12 01 19 01 B3 01 12 01 19 01
S,0,4B0,8, 84 B0 4E A5 00 43 00 64 84 B0 4E A5 00 43 00 64 4E A5 00 43 00

关于mysql - SQL 在不同的字节中拆分 hexastring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46640698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com