question
question

Reputation: 31

c++ and c# convert byte-string

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

Upvotes: 3

Views: 405

Answers (1)

CodesInChaos
CodesInChaos

Reputation: 108800

"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);

Upvotes: 8

Related Questions