Converting string to equivalent byte hex in c#

前端 未结 3 2049
名媛妹妹
名媛妹妹 2021-01-29 14:08

I have an incoming string 68016101061B4A60193390662046804020422044204000420040402060226024676DB16 and I want to convert into 0x68 0x01 0x61 0x01 0x06 0x1B 0x4

3条回答
  •  失恋的感觉
    2021-01-29 14:52

    If you want to use pure LINQ, you can do something along the lines of

    data
        .Select((x,i) => (Index: i, Value: x))
        .GroupBy(tuple => tuple.Index / 2, tuple => tuple.Value, (_,values) => "0x" + string.Join("",values))
        .Select(x => Convert.ToByte(x, 16))
        .ToArray();
    

    if you want a ridiculous one-liner.

提交回复
热议问题