C ++ and C # convert byte strings

I am dealing with a problem. I have the line "00-C4-D3-EC-12-45". I want to fill an array of bytes with these values. How to do it? I mean that byte [] x = new byte [6] has values: 0x00, 0xC4; 0xD3 ... and so on. Need a solution. thank

+3
source share
1 answer
"00-C4-D3-EC-12-45".Split('-').Select(s=>Convert.ToByte(s, 16)).ToArray();

Or without LINQ:

string[] parts="00-C4-D3-EC-12-45".Split('-');
byte[] bytes=new byte[parts.Length];
for(int i=0;i<bytes.Length;i++)
    bytes[i]=Convert.ToByte(parts[i], 16);
+8
source

Source: https://habr.com/ru/post/1784969/


All Articles