Skip to content

Instantly share code, notes, and snippets.

@cuteribs
Created May 24, 2023 06:10
Show Gist options
  • Select an option

  • Save cuteribs/2247fd0c2db3701f9aa43f5be1b5da08 to your computer and use it in GitHub Desktop.

Select an option

Save cuteribs/2247fd0c2db3701f9aa43f5be1b5da08 to your computer and use it in GitHub Desktop.
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