I am trying to split a hexadecimal string, 3E8.8F5C28F5C28
, into individual bytes. I tried the following code to split the string:
const string = "3E8.8F5C28F5C28";
const res = string.split(/([dw]{2})/).filter(e => e);
But it gave me this result:
[
'3E', '8.', '8F',
'5C', '28', 'F5',
'C2', '8'
]
I need to split the given hex string like 03 E8 8F
. I don’t want the fractional values after 8F.
How to achieve this?
2
Answers
It’s an easy task do it like this:
You could first pad the string so it has an even number of hex digits in both the integer as fractional parts. Then clip the fractional part to just 2 digits, and finally match all digit pairs.