Created
May 24, 2023 06:10
-
-
Save cuteribs/2247fd0c2db3701f9aa43f5be1b5da08 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Globalization; | |
| public static byte[] ParseRedisHexData(string hexData) | |
| { | |
| var bytes = hexData.Split("\\x", StringSplitOptions.RemoveEmptyEntries) | |
| .SelectMany(ParseRedisHexSegment) | |
| .ToArray(); | |
| return bytes; | |
| IEnumerable<byte> ParseRedisHexSegment(string segment) | |
| { | |
| if (segment.Length < 2) throw new System.ArgumentOutOfRangeException(segment); | |
| yield return byte.Parse(segment[..2], NumberStyles.HexNumber); | |
| if (segment.Length > 2) | |
| { | |
| foreach (var c in segment[2..]) yield return (byte)c; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment